• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with double data type  RSS feed

 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have this method that is supposed to input a double, but when I input a value with a decimal point, it gives incorrect results (it reverts to the default zero specified in the catch block in the method). I did not write this method - it has been defined by competent programmers (I believe), so I'm guessing I'm using it incorrectly... Can anyone see what I'm doing wrong?

First the method:


Next, to a bit of code using the method:



This gives the result: "You entered a 0.0.".

Would appreciate any hints!
Thanks
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I see it calls the method input(String prompt).

Here's that method:



I think that's all...
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to recall that System.in should always be used with a buffer size of 1 when wrapped in a BufferedReader, i.e.
new java.io.BufferedReader(new java.io.InputStreamReader(System.in)), 1)
I'm not sure that's the problem here, though. Instead of ignoring the exception, why don't you print it out so you can see for yourself what's going wrong?
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I tried that - it doesn't give me an error. When I enter just a 5, it gives me 5.0, but when I enter a decimal point number, e.g. 5.0, it doesn't take it, and gives me 0.0 instead.

Is this how I might output the error message?

 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This tells me it's a numberformatexception. I wonder if the problem is in the line:


To me, it seems illogical to try to "convert" to a long from a string containing a period...

Maybe I'm not understanding the code very well.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Flo Powers:
it has been defined by competent programmers (I believe)


I think that this is the problem -- you're beleiving that they are competent. To enumerate the problems with this method:



1. Of most immediate concern, they are returning a long instead of a double. Java accepts this because it can implicitly cast from a long to a double, but longs do not contain fractional information. Not only that, but the parsing of a string containing a decimal point with the Long.valueOf method is wrong. You were correct to say that this is illogical.

2. There is no exception handling -- nothing to tell you what went wrong. It just silently returns a 0.0. You were correct in that you want to display the error message, but the most common form is:

In this case, however, the exception should either be thrown to be handled further up the stack, or the user should be resented with a message stating that their number was invalid and they must re-enter it.

3. The code formatting is really poor (this is most times a personal thing, but in a heated debate -- um, er, friendly discussion -- here at the Ranch, this specific style of formatting was almost universally labeled "bad"). It makes reading the method difficult.

The method probably should look like:



This separates the input from the parsing (makes for easier debugging), throws the exception, has better formatting, and, most importantly, returns a double.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, if you can't throw the exception, this would also work:

 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That makes a lot more sense to me. I have used String input and then Double.parseDouble in the past, but was required to use this new class with I/O methods. Most of them worked perfectly well, but this one caused a hickup. I will change the definition.

Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!