wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes a program to check input is integer, float, or NAN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a program to check input is integer, float, or NAN" Watch "a program to check input is integer, float, or NAN" New topic
Author

a program to check input is integer, float, or NAN

kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Hello,
I am working on a program that inputs a data into a textfield and checks if the number is integer, float or Not a number. i have gotten the layout. But i have a rough idea that if it is an integer there would be no decimal points and decimal point for a float data and i fuser enters characters like "A" or "2M" then it's not a number. But this is my first class with strings and so have no idea how to approach this problem. Any guideline appreciate..I have just gotten a layout.


thanx,
kt
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
You're on the right track. Check the String API; you'll find a few methods, like indexOf, that will help you solve the problem.

Also, if you create an Integer or Float object with an invalid argument (such as, not a number), you get an exception that may also help you solve your problem...


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
First you could check for a Float, which eliminate or verify it containing characters, then you could check the Float to see if it's an Integer. Other ways of doing it, this would just be one (and maybe not the best one).

------------------
Shameless post just in hopes of winning a book!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

just because it has a decimal, doesn't mean it's a float. what if it has TWO decimal points???


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
That's where you could use the exception processing...
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Jeff Bosch:
That's where you could use the exception processing...


Thanx for your reply, But i am not exposed to exception handling at all. I did try to do using isDigit() method, but getting a lot of uncomprehensible errors.

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

for(int x = 0; x < letters.length; x++)
{

if(isDigit(letters[x]) == true)
g.drawString("Integer", 50, 50);



}
I got up to the above code but don't know why keep getting tone of errors. I am very recently introduced to Strings and having a tough time with it.

I also tried to use indexOf() and lastIndexOf() methods to checked to see if they are equal then there is only one decimal point, if not more than one and can say not a number. But i couldn't get past the compilation.
Help greatly appreciated..
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
isDigit is a static method of Character, so you must invoke it like this:



It returns a boolean result, which you can use as a flag. The argument can be a char returned from cycling through the string...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It will be a good thing to learn about exceptions, now or later. Your code would look something like:

"try" means we're entering a block of code that might throw an exception and we plan to handle it. If the Integer.parseInt method throws an exception (see the doc to see what kind it might throw) we'll jump to the catch block, otherwise we'll skip the catch block. So if the string is a valid integer, like "123", we'll do the parseInt line and then set the variable isInteger to true and we're done. If the string is not a valid integer then Integer will throw an exception, we'll jump right by setting isInteger to true, catch the exception and set isInteger to false.

If you're totally new to exceptions, try a good Java reference like Thinking in Java for more details. They are way more interesting and valuable than what I showed.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: a program to check input is integer, float, or NAN