0

I have a do while loop. Checking x is between two values. Now im supposed to be taking in an int value, but if the user types a double im getting exceptions. How do I incorparate a check in the same if statement so that if the user types a double it would print something like "x must be an int between 10 and 150:"

            do {
            x = sc.nextInt();
            if ( x < 10 || x > 150 ) {
                System.out.print("x between 10 and 150: ");
            } else {
                break;
            }
7
  • 5
    Why not just use nextDouble instead of nextInt? Then check whether the specified value is equal to its integer equivalent? Admittedly that would allow "100.000" as valid input - is that a problem? Commented Nov 6, 2013 at 15:27
  • 2
    I don't know if this is a typo, but if you want your x to be between 10 and 50, your condition is wrong. You should change it to x > 10 || x < 150 Commented Nov 6, 2013 at 15:39
  • 2
    actually it should be x>10 && x<150... Commented Nov 6, 2013 at 15:41
  • 3
    @JonSkeet This doesn't solve his problem that the Scanner method may throw exceptions, after all, user could enter something like ^Chaha\b\b\bnot a number, you fool! Commented Nov 6, 2013 at 15:41
  • If the print statement is correct, the condition should actually be: x >= 10 && x <= 150 Commented Nov 6, 2013 at 15:41

3 Answers 3

4

You do not need an additional check. The exception is just there so you can act accordingly in your program. After all, it doesn't really matter how exactly the input was wrong. Just catch the exception (NumberFormatException, I guess?) and upon catching it, print an error message:

while (true) try {
    // here goes your code that pretends only valid inputs can occur
    break;   // or better yet, put this in a method and return
} catch (NumberFormatException nfex) {  // or whatever is appropriate
    System.err.println("You're supposed to enter integral values.");
    // will repeat due to while above
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can just catch the exception and handle it, using a while (true) to allow the user to re-try.

Here's my code:

Scanner sc = new Scanner(System.in);
do {
    System.out.print("\nInsert a number >>> ");
    try {
        int x = sc.nextInt();
        System.out.println("You inserted " + x);
        if (x > 10 && x < 150) {
            System.out.print("x between 10 and 150: ");
        } else {
            break;
        }
    } catch (InputMismatchException e) {
        System.out.println("x must be an int between 10 and 150");
        sc.nextLine(); //This line is really important, without it you'll have an endless loop as the sc.nextInt() would be skipped. For more infos, see this answer http://stackoverflow.com/a/8043307/1094430
    }
} while (true);

3 Comments

yep. The best one. +1
a do while never is the best solution, besides that x between 10 and 150 should be x> 10 && x < 150
I never use the do-while but the OP used it and I didn't want to change his code. Thanks for the advice on the and, I forgot to change it when I fixed his inital condition.
0
public class SomeClass {                                                         
    public static void main(String args[]) {                                     
        double x = 150.999; // 1
        /* int x = (int) 150.999; */ // 2                                          
        if ( x >= 10 && x <= 150 ) { // The comparison will still work                                             
            System.out.print("x between 10 and 150: " + x + "\n");               
        }                                                                        
    }                                                                            
}
  1. Declare x as double, comparisons between double and int will still work.
  2. Or, cast the number and any decimal values will be discarded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.