
-----------------------------------
evogre3n
Wed Nov 09, 2005 3:03 pm

Error Trap Decimal input?
-----------------------------------
My program requires input of a number that has to have a decimal value, how would I error trap against normal integers...

if (userInput == int)

that kind of thing...

-----------------------------------
wtd
Wed Nov 09, 2005 3:08 pm


-----------------------------------
When you read it, aren't you doing something like:

Integer.parseInt(keyboard.readLine())

If so, do you think it'll give you back anything other than an int?  And if it can't read an int, what do you think it does?

-----------------------------------
evogre3n
Wed Nov 09, 2005 3:15 pm


-----------------------------------
Well, i read as a double... because I expect a double input.. (a decimal value) i want to set up an error trap so that if they enter an integer, it stops and asks for input again...

-----------------------------------
wtd
Wed Nov 09, 2005 3:16 pm


-----------------------------------
Ok.  So you have your double.

What happens if you convert that to an integer?

If the double is actually just an integer, then I'd imagine that the new integer value would be equal to the original double value.

-----------------------------------
evogre3n
Wed Nov 09, 2005 3:23 pm


-----------------------------------
Not following...

i need the input to be lets say... 20.123

if they enter just 20

or just 123

or just a normal integer with no decimals, i want an error to come up, but I have no idea how to trap that...

-----------------------------------
wtd
Wed Nov 09, 2005 3:27 pm


-----------------------------------
Ok.  One more time.

Let's say you get 20.123.

Converting that to an integer, we get 20.  20 is not equal to 20.123, so 20.123 must be a floating point number, right?

Conversely, if we get 20, and convert that to an integer, we get 20.  20 is equal to 20, so it is not a floating point number.

Alternatively, read in the input as a String.  Store that in a variable.  Use Double.parseDouble to get your value.  Also run Integer.parseInt on the same string.  If Integer.parseInt succeeds, then the user input an integer value.

-----------------------------------
evogre3n
Thu Nov 10, 2005 7:39 am


-----------------------------------
gotyaa  :D 

unfortunately my assignment requires it to be a double and it cant be a string... its alright, what i want is a bit extra, so im good for now.

Thanks.  :)

-----------------------------------
wtd
Thu Nov 10, 2005 2:08 pm


-----------------------------------
You can have it both ways.  

When you read in the input, it starts life as a String anyway, and then you have to go to extra effort to make if a double.

You can just as easily separately store that String and try converting it to an int.

-----------------------------------
MysticVegeta
Fri Nov 11, 2005 11:46 am


-----------------------------------
Isn't there a  strintok command that returns a boolean value for Java? I am sure there should be

-----------------------------------
wtd
Fri Nov 11, 2005 2:07 pm


-----------------------------------
There is equivalent functionality, yes.

int number;

while (true)
{
   try
   {
      number = Integer.parseInt(keyboard.readLine());
      break;
   }
   catch (NumberFormatException e)
   {
      System.err.println("Bad input, try again.");
   }
}

-----------------------------------
rizzix
Fri Nov 11, 2005 6:39 pm


-----------------------------------
even better (1.5 only)...

Scanner sc = new Scanner(inputStream);
if (sc.hasNextInt())
    number = sc.nextInt();
else
    throw new SomeException();
