Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just because it has a decimal, doesn't mean it's a float. what if it has TWO decimal points???
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's where you could use the exception processing...
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic