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.
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!
} 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..
Joined: Jul 30, 2003
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...
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