How to check if a String input is integer, float or NAN

K

kt

Hello folks,
I tried to do get so far with my code by using isDigit()
method but don't know how to use it for float. This is my code. Also i
am
not exposed to exceptions at all. So is there a method that i could use
without using exceptions. The following code works fine if i type an
integer number, or a character but if type something like "7A" it gets
messed up instead of saying "NAN". I don't know how to check for float
and
invalid float like " 1.3.6". Any help greatly appreciated.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CheckNum extends Applet
implements ActionListener
{

private TextField inputTF;
private String inputVal;
private boolean isInteger;

public void init()
{
Label textLbl = new Label("A program to input a string and
display the
type of the number");
inputTF = new TextField(7);

add(textLbl);
add(inputTF);

inputTF.addActionListener(this);

}

public void actionPerformed(ActionEvent event)
{
if(event.getSource() == inputTF)
inputVal = inputTF.getText().trim();

repaint();
}


public void paint(Graphics g)
{
char [] letters;
letters = inputVal.toCharArray();

for(int x = 0; x < letters.length; x++)
{
if(Character.isDigit(letters[x]) == true)
g.drawString("Integer", 50, 100);

else
g.drawString("NAN", 50, 100);



}



}
}
 
W

Wendy S

kt said:
Hello folks,
I tried to do get so far with my code by using isDigit()
method but don't know how to use it for float. This is my code. Also i
am not exposed to exceptions at all. So is there a method that i could use
without using exceptions.

If you're going to program in Java, you need to learn about exceptions.
Here:
http://java.sun.com/docs/books/tutorial/essential/exceptions/

I'd probably just cheat and try Integer.parseInteger(...). If it throws an
exception, (IIRC NumberFormatException, check the javadocs,) then it wasn't
an integer.
 
J

Johan Poppe

kt skrev:
Hello folks,
I tried to do get so far with my code by using isDigit()
method but don't know how to use it for float. This is my code. Also i
am
not exposed to exceptions at all. So is there a method that i could use
without using exceptions.

Why don't you want to "be exposed to" exceptions? You mean you haven't
gotten to exceptions in your java course?
The following code works fine if i type an
integer number, or a character but if type something like "7A" it gets
messed up instead of saying "NAN".

Your current code writes a message for every single digit. So for a
string like 7A, you first write that it's an integer, and then write
that it's NAN. To further complicate, you write directly on the
canvas, and therefore will see both messages painted on top of each
other.

To fix this, you should move the output out of the loop. Instead of
writing for each digit, keep track of a flag in the loop, and then
write afterwawrds.
I don't know how to check for float and
invalid float like " 1.3.6".

To check that, you'll have to accept a character if it's either a
digit, or if it is the decimal separator, but not if the string
already contained a decimal separator. This means your loop contents
get slightly more complex, but not overly so. I'll leave the details
to you :)


As a general tip, I advice you to split your logic (checking if a
string represents an integer) from your user interface code (accepting
input and writing output). Having your logic in the paint() method is
really really bad design. Also, awt classes like Label and TextField
is nice. (Or their Swing counterparts.)
 
B

Betty

kt said:
Hello folks,
I tried to do get so far with my code by using isDigit()
method but don't know how to use it for float. This is my code. Also i
am
not exposed to exceptions at all. So is there a method that i could use
without using exceptions. The following code works fine if i type an
integer number, or a character but if type something like "7A" it gets
messed up instead of saying "NAN". I don't know how to check for float
and
invalid float like " 1.3.6". Any help greatly appreciated.

Back in the 1970's there was a lot of talk about using a table for this.
The basic idea is that each row represented a 'state' that your program
was in and each 'column' represented a possible input character. The
value in the table was the name of a method you would call. Somewhere
in the method you would update the 'state'. This makes the core of the
program trivial with all the intelligence in the table and methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top