简体   繁体   中英

Why am I still getting a InputMissmatchException even though i have a catch statement

System.out.print("What kind of array do you want to create?\n1. Integer Array\n2. Double Array\n3. String Array\nYour Answer: ");
String input;
int num1 = 0;
try {
    input = s.next();
    num1 = Integer.parseInt(input);
    while (num1 > 3 || num1 < 1) {
        System.out.print("Please enter one of the three available options.\nYour Answer: ");
        input = s.next();
        num1 = Integer.parseInt(input);
    }
} catch (InputMismatchException e) {
    System.out.println("Do not enter a letter/special character");
}

So I am basically making a question for the user asking him what kind of array he wants to create. But, when i try to break it and put in a Char / String I till get an error and the program exits.

Add the try-catch block inside your while loop. Otherwise the exception is catched after the loop and when you process the exception (in the catch block) you continue with the flow without asking the user to try again.

That's not what causes your problem though. If you want to just print error and continue then you should switch your code to nextInt() instead of next() and parseInt() . Then the exception will be correct and it will be easier to read. (Currently you probably get NumberFormatException when you try to parse String to Int instead of exception on input - if you want to do it like that then change the exception you try to catch)

int num1 = 0;
try {
    num1 = s.nextInt();
    while (num1 > 3 || num1 < 1) {
        System.out.print("Please enter one of the three available options.\nYour Answer: ");
        num1 = s.nextInt();
    }
} catch (InputMismatchException e) {
    System.out.println("Do not enter a letter/special character");
}

s.next() reads a String from the Scanner . Therefore, if you type in a non-numeric String , it doesn't throw InputMismatchException . Instead, Integer.parseInt throws NumberFormatException when trying to parse that String as an int , and you don't catch that exception.

You might want to try something like this:

Scanner s = new Scanner (System.in);
System.out.print("What kind of array do you want to create?\n1. Integer Array\n2. Double Array\n3. String Array\nYour Answer: ");
String input;
int num1 = 0;
input = s.next();
try {
  num1 = Integer.parseInt(input);
}
catch (NumberFormatException numEx) {
  System.out.println("Do not enter a letter/special character");
}
while (num1 > 3 || num1 < 1) {
  System.out.print("Please enter one of the three available options.\nYour Answer: ");
  input = s.next();
  try {
    num1 = Integer.parseInt(input);
  }
  catch (NumberFormatException numEx) {
    System.out.println("Do not enter a letter/special character");
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM