Reputation: 595
I have this code and I want to catch the letter exception but it keeps having these errors:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at exercise_one.Exercise.main(Exercise.java:17)
And here is my code:
System.out.print("Enter the number of students: ");
students = input.nextInt();
while (students <= 0) {
try {
System.out.print("Enter the number of students: ");
students = input.nextInt();
}
catch (InputMismatchException e) {
System.out.print("Enter the number of students");
}
}
Upvotes: 8
Views: 96087
Reputation: 1
You should take the user input inside the try section not outside, and that's how you can solve this problem
Here is an example:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter a number to create table");
Scanner scanner = new Scanner(System.in);
try {
int userInput = scanner.nextInt();
int i = 0;
while (i<10) {
i++;
System.out.println(i*userInput);
}
} catch (InputMismatchException e) {
System.out.println("Enter a valid number ");
}
}
}
Upvotes: 0
Reputation: 917
You can use a do-while loop instead to eliminate the first input.nextInt()
.
int students = 0;
do {
try {
// Get input
System.out.print("Enter the number of students: ");
students = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("Invalid number of students. ");
}
input.nextLine(); // clears the buffer
} while (students <= 0);
// Do something with guaranteed valid value
Therefore all InputMismatchException
can be handled in one place.
Upvotes: 12
Reputation: 11
John Stef,
The method nextInt() just Throws the following exceptions:
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
If you need/want throw another one type of exception you got to specify your own exception. Use throw statement. Here's an example of a throw statement.
throw someThrowableObject;
For example:
try {
System.out.print("Enter the number of students: ");
students = input.nextInt();
if(students <=0){
throw new Exception("Null or Negative number of students is invalid.");
}
} catch (InputMismatchException e) {
System.out.print("Invalid input. Please enter a number for student number.");
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
This will catch the both mismatch and negative exceptions.
Although the do... while posted by Siyu Song achieve the desired input from the user, it don't catch negative int exceptions as you wish.
You can use this try and do...while from Siyu Song to achieve what you wanting. The complete code looks like this:
do {
try {
System.out.print("Enter the number of students: ");
students = input.nextInt();
if(students <=0){
throw new Exception("Negative number of students is invalid.");
}
} catch (InputMismatchException e) {
System.out.print("Invalid input. Please enter a number for students number.");
} catch (Exception e) {
System.out.print(e.getMessage());
}
input.nextLine();
} while (students <=0);
Upvotes: 0
Reputation: 1
If you wanna be sure about being integer for all input you can try this:
while(true) {
try {
System.out.print("Kolon sayısını giriniz: ");
c = scan.nextInt();
} catch (Exception e) {
System.out.print("Geçersiz giriş.. ");
scan.nextLine();
continue;
}
break;
}
// or this...
while(true) {
System.out.print("Give me a number");
try {
input = scan.nextInt();
break;
} catch (Exception e) {
System.out.print("There was mismatch");
scan.nextLine();
}
}
Upvotes: 0
Reputation: 167
Reading data from Scanner and assigning it to Int type. Since you are supplying String this will throw exception. To handle this situation you must write your snippet inside Try- Catch block only.
Upvotes: 0
Reputation: 54672
from the doc
Scanner.nextInt Scans the next token of the input as an int. if the next token does not match the Integer regular expression, or is out of range
So it seems you are not entering any integer as input.
you can use
while (students <= 0) {
try {
System.out.print("Enter the number of students: ");
students = input1.nextInt();
}
catch (InputMismatchException e) {
input1.nextLine();
}
}
Upvotes: 4