Home > Software design >  Handle InputMismatchException exception but doesn't catch it Java
Handle InputMismatchException exception but doesn't catch it Java

Time:04-03

For some unknown reason, when the user input is a string or special characters it doesn't catch my exception (the scanner accepts only integers and from 1 to 3). The code is the below:

package mainpackage;
import java.util.InputMismatchException;
import java.util.Scanner;

public class CreateUsers {
    
    public static void main(String[] args) {
        
        Scanner chooseNumber = new Scanner(System.in);
        int number;
        
        System.out.println("Welcome to UniStudents Application!\n"
                  "Please select how to Log in: \n"
                  "1. Log in as a Student \n"
                  "2. Log in as a Professor \n"
                  "3. Log in as a Secretary \n"
                  "To choose an action, please write the number of the action: ");  
        number = chooseNumber.nextInt();
        
        while(true) {
            if(number > 3 || number < 1) {
                System.out.println("The number of the action does not exist.\n"
                         "Please try again: ");
                chooseNumber.nextInt();
            }
            else {
                System.out.println("ok");
                chooseNumber.close();
                break;
            }   
        }
        try{
            chooseNumber.nextInt();
        }
        catch(InputMismatchException e) {
             
            System.out.println("Please write the number of the action you want to choose \n"
                      "Please select how to Log in: \n"
                      "1. Log in as a Student \n"
                      "2. Log in as a Professor\n"
                      "3. Log in as a Secretary\n");
        }

The result of the code is that:

1. Log in as a Student 
2. Log in as a Professor 
3. Log in as a Secretary 
To choose an action, please write the number of the action: 
$^&*
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at JavaBasics/mainpackage.CreateUsers.main(CreateUsers.java:18)

I can't figure it out

CodePudding user response:

The first call to number = chooseNumber.nextInt(); is not in the try block, so the exception isn't caught. Move the beginning of the try block above it and you should be fine.

CodePudding user response:

Because this exception is thrown at line 18, which is number = chooseNumber.nextInt(); above while (true), and it is not covered by the try-catch block, try:

package mainpackage;
import java.util.InputMismatchException;
import java.util.Scanner;

public class CreateUsers {
    
    public static void main(String[] args) {
        
        Scanner chooseNumber = new Scanner(System.in);
        int number;
        
        System.out.println("Welcome to UniStudents Application!\n"
                  "Please select how to Log in: \n"
                  "1. Log in as a Student \n"
                  "2. Log in as a Professor \n"
                  "3. Log in as a Secretary \n"
                  "To choose an action, please write the number of the action: ");  
        try {
            number = chooseNumber.nextInt();
        
            while(true) {
                if(number > 3 || number < 1) {
                    System.out.println("The number of the action does not exist.\n"
                             "Please try again: ");
                    chooseNumber.nextInt();
                }
                else {
                    System.out.println("ok");
                    chooseNumber.close();
                    break;
                }   
            }
            chooseNumber.nextInt();
        } catch (InputMismatchException e) {
             
            System.out.println("Please write the number of the action you want to choose \n"
                      "Please select how to Log in: \n"
                      "1. Log in as a Student \n"
                      "2. Log in as a Professor\n"
                      "3. Log in as a Secretary\n");
        }
  • Related