Jay Rex wrote:1. Why is it that if I enter 99.99, it enters the if loop and prints "Please enter a valid number". 99.99 is not greater than 99.99.
Jay Rex wrote:2. I am new to try catch blocks. I see that they have block scope. Lines 18 and 24 are duplicated, as it appears I have to check the code inside the try block and then duplicate the code outside the try catch block because that is what I want to do. Is this the correct way to do it?
Paweł Baczyński wrote:
Read carefully what condition you have in line 25. Write on pensil your calculation for input 99.99.
Paweł Baczyński wrote:
Also, I noticed that you use BigDecimal(double) constructor. Read the warning in corresponding javadoc so you are not surprised in the future.
All things are lawful, but not all things are profitable.
Paweł Baczyński wrote:Welcome to the Ranch!
Paweł Baczyński wrote:It is not the correct way. If no exception is thrown then the value of result will already be set to desired value. No need to duplicate this. In this case the computation will be quick so no harm done, but if it were an expensive operation you might have a problem.
Campbell Ritchie wrote:Welcome to the Ranch
Campbell Ritchie wrote:Don't use next() and try doing converting the result. Use next() when you want to get one token and use it as text. If you want a BigDecimal from a Scanner, that is what you should ask for: look here.
Dave Tolls wrote:This, though, is the cause of point (1).
Knute Snortum wrote:If you're still stumped, try printing the values of upperLimit and lowerLimit.
Dave Tolls wrote:That check is correct as far as I can see.
All things are lawful, but not all things are profitable.
Campbell Ritchie wrote:Who needs an exception?
Campbell Ritchie wrote:Avoid System.exit
Knute Snortum wrote:It looks like you still have the problem of rounding your input but not your upper and lower limits.
Campbell Ritchie wrote:Do you need new BigDecimal("0") when you can use this?
You are comparing a boolean from a hasNextXXX() method to a BigDecimal from the nextXXX() method; that won't compile.You need to compare nextBigDecimal() against the limits. So you change it to this:-. . . and it will compile. I am keeping quiet about the serious error hidden in that code, however.Jay Rex wrote:. . . The following code doesn't work, but I want to try and condense the logic further by doing the following:
. . .
Jay Rex wrote:Why is it that if I enter 99.99, it enters the if loop and prints "Please enter a valid number". 99.99 is not greater than 99.99.
Knute Snortum wrote:If you're still stumped, try printing the values of upperLimit and lowerLimit.
Jay Rex wrote:
Knute Snortum wrote:It looks like you still have the problem of rounding your input but not your upper and lower limits.
Jay Rex wrote:For this small program, rounding the input is appropriate.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:I'm not sure what the length of you program has to do with it, but not rounding the upper and lower limit causes the problem you originally asked about.
Campbell Ritchie wrote:Rob Spoor taught me that you can avoid an IME with a simple loop:-
All things are lawful, but not all things are profitable.
I can see nothing wrong with your loop. Unless you live somewhere not using a full stop as the decimal point. Please give us the full details of what goes wrong.Jay Rex wrote:. . . Unfortunately, the loop doesn't work. It only works for int input, not for floating point input. . . .
Knute Snortum wrote:Hmm.. it works for me. Are you using it like this?
Campbell Ritchie wrote:I can see nothing wrong with your loop. Unless you live somewhere not using a full stop as the decimal point. Please give us the full details of what goes wrong.
Campbell Ritchie wrote:It does sound like a locale issue, which is why the full stop didn't help.
So you can work out the locale from that. Try moving your Locale: System.setProperty("user.country", "GB"); System.setProperty("user.language", "en");java PropertiesList
The property awt.toolkit . . .
The property user.country is GB
. . .
The property user.language is en
. . .
Stephan van Hulst wrote:It's probably not.
All things are lawful, but not all things are profitable.
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
|