Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Error Trap Decimal input?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evogre3n




PostPosted: Wed Nov 09, 2005 3:03 pm   Post subject: 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...

code:
if (userInput == int)


that kind of thing...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Nov 09, 2005 3:08 pm   Post subject: (No subject)

When you read it, aren't you doing something like:

Java:
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




PostPosted: Wed Nov 09, 2005 3:15 pm   Post subject: (No subject)

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




PostPosted: Wed Nov 09, 2005 3:16 pm   Post subject: (No subject)

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




PostPosted: Wed Nov 09, 2005 3:23 pm   Post subject: (No subject)

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




PostPosted: Wed Nov 09, 2005 3:27 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 10, 2005 7:39 am   Post subject: (No subject)

gotyaa Very Happy

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. Smile
wtd




PostPosted: Thu Nov 10, 2005 2:08 pm   Post subject: (No subject)

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.
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Fri Nov 11, 2005 11:46 am   Post subject: (No subject)

Isn't there a
Turing:
command that returns a boolean value for Java? I am sure there should be
wtd




PostPosted: Fri Nov 11, 2005 2:07 pm   Post subject: (No subject)

There is equivalent functionality, yes.

Java:
int number;

while (true)
{
   try
   {
      number = Integer.parseInt(keyboard.readLine());
      break;
   }
   catch (NumberFormatException e)
   {
      System.err.println("Bad input, try again.");
   }
}
rizzix




PostPosted: Fri Nov 11, 2005 6:39 pm   Post subject: (No subject)

even better (1.5 only)...

Java:
Scanner sc = new Scanner(inputStream);
if (sc.hasNextInt())
    number = sc.nextInt();
else
    throw new SomeException();
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: