Question
Hello, thank you very much for answering me. Execution errors In the following code we want to ask the user for an integer and store
Hello, thank you very much for answering me.
Execution errors In the following code we want to ask the user for an integer and store it in a variable.
The Scanner class allows us to obtain the input that the user writes with the nextInt() method (in this case it has to be an integer).
The nextInt() method throws an InputMismatchException exception if the input entered by the user is not an integer or is out of range.
import java.util.Scanner;
public class inputInt {
public static void main (String args[]){
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt();
}
}
The code has no compile errors and seems to work fine. But will it work correctly (storing the value entered by the user) with the following inputs?
13859
It works!
Throw an exception.
1243.2
It works!
Throw an exception.
91243646443487979664376974314679741
It works!
Throw an exception.
12421a
It works!
Throw an exception. 12
It works!
Throw an exception. Hello1
It works!
Throw an exception.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started