1

I have written a program for one of my assignments which asks for an input to assign to an integer variable. One of the parts of the assignment is, if the integer input is anything other than 1, 2, 3, or 4, the program should print a statement saying that is an invalid input, then end the program.

I have figured out how to get it to print this statement if the user enters an input higher than 4 or lower than 1, but I can not figure out how to get the same result when a double is entered.

I have managed to make it work by using a while loop that checks if the input is an integer or not,

while (!input.hasNextInt()){
    System.out.println("Invalid choice. Good-bye.");
    input.next();
}
int selection = input.nextInt();

I would like it to recognize it as an invalid input and then end the program right there. Instead, it waits for another valid input from the user

Expected result, per the assignment: " If the user enters anything other than 1, 2, 3, or 4, the program should print, "Invalid choice. Good-bye." "

4
  • ...while(condition) checks a "condition" (where condition is a "boolean expression/value") repeatedly ...an if(condition) checks the condition once ;) Commented Jan 29, 2019 at 20:51
  • @xerx593 the output i got from my if(condition) for integers not 1/2/3/4 was "Invalid choice. Good-bye. BUILD SUCCESSFUL (total time: 2 seconds)" whereas when changing the while to an if for the code in the top post, it gives me "Exception in thread "main" java.util.InputMismatchException" as an error Commented Jan 29, 2019 at 20:58
  • an alternative to a while loop is a for loop. however, you want to double check your logic here. "while the input does not have another int to parse in, you will continue to loop" is not correct. you want "while the input does have another int to parse in, continue to loop". doesn't matter if you use a while or a for loop, you need to get your logic correct Commented Jan 29, 2019 at 21:02
  • 1
    You use hasNextInt and that means that code automatically check was the entered symbol an Integer and if not (in case of Double or Chars) the code will result in an Exception warning and the code running stops. To avoid it surround your code with a try-catch block. In this case you'll catch wrong symbols (not Integers in your case) and can break your code or print any warning message, or let code go further. Commented Jan 29, 2019 at 21:27

1 Answer 1

1

You did not mention what happens when a valid number is entered or the user enters anything other than an integer number.
So this code will exit the program when the user enters a non integer value or an integer not between 1 and 4:

Scanner input = new Scanner(System.in);
int number = 0;
boolean valid = false;
System.out.println("Enter number: ");
if (input.hasNextLine()) {
    if (input.hasNextInt()) {
        number = input.nextInt();
        if (number >= 1 && number <= 4) {
            valid = true;
            System.out.println("Valid choice:" + number);
        }
    }
    input.nextLine();
    if (!valid) {
        System.out.println("Invalid choice. Good-bye.");
        System.exit(0);
    }
}
Sign up to request clarification or add additional context in comments.

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.