0

I want to check if the input my code needs is correct so I put a lot of if statements checking for the requirements and I can't figure out why it's not working. It is supposed to check if n is less or equal to 91 and / or it is a decimal (I don't want my input to be either). This is so that the user doesn't break the program by typing a decimal or a number higher than 91.

while (Error == 1) {
    n = user_input.nextDouble();
    if ((n - Math.round(n) <= 0.9) && (n - Math.round(n) >= 0.1)) {
        System.out.println("Error: No Decimal points please, try again");
        continue;
    }
    if ((n - Math.round(n) <= 0.9) && (n - Math.round(n) >= 0.1) && (n > 91)) {
        System.out.println("Error: No Decimal points please, try again");
        System.out.println("Error: Number too high, try again");
        continue;
    }
    if (n > 91) {
        System.out.println("Error: Number too high, try again");
        continue;
    }
    if (n == Math.round(n)) {
        Error = 0;
    }
    if (n == 0) {
        break;
    }
}

For some reason when I type 9.1 or 9.9 it doesn't do anything at all. It's blank... I did >= which is supposed to check if it is bigger or equal to and <= which is supposed to check if it is less or equal to. Is that wrong?

Shahid
  • 2,288
  • 1
  • 14
  • 24
TheLittleCoder
  • 21
  • 2
  • 11

2 Answers2

0

In case of 9.1 and 9.9, none of the conditions are satisfied. That's why nothing is done. Loop is iterated and wait for next double input.

Here, the main culprit is n - Math.round(n). The calculations are not being accurate. For example, in case of 9.1:

n - Math.round(n) value is equal to 0.09999999999999964. So, the condition n - Math.round(n) >= 0.1 is never satisfied and no if block is reached.

Shahid
  • 2,288
  • 1
  • 14
  • 24
0

Well, first of all, you seem to want only inputs that are integers less than or equal to 91. It seems strange that you would say this, but then explicitly grab doubles with the nextDouble() method of Scanner.

There are better ways of checking for integers... see this question What's the best way to check to see if a String represents an integer in Java?

Either way, I'll assume you intend on sticking with your innovative methodology:

You are correct, that in regular math, 9.1 rounds to 9 and the difference between the two is less than or equal to 0.1. Your cases should work.

But welcome to the world of Java floating point algebra! Doubles don't compare well here. What do I meant that they don't compare well? Well, the difference between your '9.1' and '9' is actually 0.09999999999999964, not 1. Java doesn't compensate for this when you use basic comparators, so your comparison fails.

Hope is not lost! There is a better way of comparing doubles than using the regular comparison operators. Introducing.... Double.compare()!! You can read the javadocs on that method, or you can go here for information on what that method does: some reliable tutorial site

However, what if they input 9.0001? Your test fails, even if the comparison works as you'd expect. You really should rethink your math here. As in, try this instead:

Double.compare((n - Math.round(n)),0.0) != 0)
Community
  • 1
  • 1
Jeutnarg
  • 1,138
  • 1
  • 16
  • 28
  • 1
    Thanks!!! I didn't know that java does that instead of the regular way or that there is a Comparing method for doubles! You REALLY helped! – TheLittleCoder Aug 26 '16 at 17:32
  • 1
    When I type in a number with 19 digits it does this: Error: No Decimal points please, try again. Why is it doing that instead of this?: Error: Number too high, try again – TheLittleCoder Aug 28 '16 at 22:20
  • at ~19 digits, you're reaching the limits of double precision in Java with the Math.round method. If you debug the code, you'll see that 12345123451234512345 rounds (in Java algebra) to 3.1217514143797371E18, which is too far off to be compared, even with Double.compare. You'll have to either use BigDecimal or use a different method of checking to see if your input is an integer or not. For what it's worth, 1234567899876543211 (19 digits) does work. – Jeutnarg Aug 29 '16 at 16:08