[英]How to overcome Exception in thread “main” java.util.InputMismatchException
Program:程序:
import java.util.Scanner;
public class JavaExercise1 {
static int sum, num1, num2;
static Scanner scan = new Scanner(System.in);
public static void add(){
System.out.print("Enter first number to be added: ");
num1 = scan.nextInt();
System.out.print("Enter second number to be added: ");
num2 = scan.nextInt();
sum=num1+num2;
System.out.println(num1+" + "+num2+" = "+sum);
}
static void sub(){
System.out.print("Enter first number to be substracted: ");
num1 = scan.nextInt();
System.out.print("Enter second number to be substracted: ");
num2 = scan.nextInt();
sum=num1-num2;
System.out.println(num1+" - "+num2+" = "+sum);
}
static void mul(){
System.out.print("Enter first number to be multiplied: ");
num1 = scan.nextInt();
System.out.print("Enter second number to be multiplied: ");
num2 = scan.nextInt();
sum=num1*num2;
System.out.println(num1+" x "+num2+" = "+sum);
}
static void div(){
try{
System.out.print("Enter first number to be divided: ");
num1 = scan.nextInt();
System.out.print("Entr second number to be divided: ");
num2 = scan.nextInt();
sum=num1/num2;
System.out.println(num1+" / "+num2+" = "+sum);
}catch(Exception e){
System.out.println("\nException occured...");
System.out.println(e);
}
}
static void printInstructions(){
System.out.println("1. Addition\n2. Substraction\n3. Multiplication\n4. Division\n5. Exit");
}
public static void main(String[] args) {
int ch;
do{
System.out.println("\nEnter your choice: (Choose 0 to print instructions)");
ch = scan.nextInt();
switch(ch){
case 0: printInstructions();
break;
case 1: add();
break;
case 2: sub();
break;
case 3: mul();
break;
case 4: div();
break;
case 5: break;
default:
System.out.println("Invalid choice....");
}
}while(ch!=5);
}
}
When I enter a number of greater digits is throws a thread exception (Exception in thread "main" java.util.InputMismatchException)当我输入更大的数字时会引发线程异常(线程“main”java.util.InputMismatchException 中的异常)
Output:输出:
Enter your choice: (Choose 0 to print instructions) 4 Enter first number to be divided: 9999999999输入您的选择:(选择 0 打印说明) 4 输入第一个要被除数的数字:9999999999
Exception occured... java.util.InputMismatchException: For input string: "9999999999"发生异常... java.util.InputMismatchException:对于输入字符串:“9999999999”
Enter your choice: (Choose 0 to print instructions) Exception in thread "main" java.util.InputMismatchException: For input string: "9999999999" at java.base/java.util.Scanner.nextInt(Scanner.java:2264) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at JavaExercise1.main(JavaExercise1.java:52)输入您的选择:(选择 0 打印说明)线程“main”中的异常 java.util.InputMismatchException:对于输入字符串:java.base/java.util.Scanner.nextInt(Scanner.java:2264) 处的“9999999999” java.base/java.util.Scanner.nextInt(Scanner.java:2212) 在 JavaExercise1.main(JavaExercise1.java:52)
Process finished with exit code 1进程以退出代码 1 结束
Your input 9999999999
is bigger than the largest int
in Java: 2147483647
.您的输入9999999999
大于 Java 中最大的int
: 2147483647
。
Change your scan.nextInt
to scan.nextLong
and also the corresponding integers to long
if you want to handle such large numbers.如果要处理如此大的数字,请将scan.nextInt
更改为scan.nextLong
并将相应的整数更改为long
。
Alternatively or additionally, use a try-catch
block to catch the exception and take appropriate action (like error message to user to try again).或者或另外,使用try-catch
块来捕获异常并采取适当的操作(如向用户发送错误消息以再次尝试)。 This can be a part of normal user input checking.这可以是正常用户输入检查的一部分。 Be very suspicious of user inputs because somebody might type "Prithviraj is the greatest!"
对用户输入非常怀疑,因为有人可能会输入"Prithviraj is the greatest!"
instead of a number, for example.例如,而不是数字。
And last but not least, welcome to the Stacks.最后但并非最不重要的一点,欢迎来到 Stacks。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.