#Program crashed

5 messages · Page 1 of 1 (latest)

undone needle
#

Hi so I have this code:

`int option;

    while(true) {
         try { //If its a number 
              option = Integer.parseInt(input.nextLine());
              if (option == 1 || option == 2 || option == 3 || option == 4 || option == 5 ) {
                  break; // Valid input, exit the loop
              } else { //If its a wrong number
                   System.out.println("Invalid choice. Please enter a number between 1 and 5:");
              }
          } catch (NumberFormatException e) { //If input is not a number 
              System.out.println("Invalid choice. Please enter a valid integer:");
  }`

When i inputted 2' the program crashed. Shouldn't it be caught by the catch?

fathom mirageBOT
#

This post has been reserved for your question.

Hey @undone needle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

ebon nova
undone needle