Error: Integers added together in successive uses of scanner class
Error: Integers added together in successive uses of scanner class
我正在做与处理异常相关的练习。在使用 Scanner class 和以下练习检查 InputMismatchExceptions 时,我从以下代码中得到了以下结果。
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.print("Enter an integer: ");
int a = getInt();
System.out.print("Enter a second integer: ");
int b = getInt();
int result = a + b;
System.out.println(result);
}
public static int getInt(){
while (true){
try {
return sc.nextInt();
}
catch(InputMismatchException e){
System.out.print("I'm sorry, that's not an integer."
+ " Please try again: ");
sc.next();
}
}
}
输出是:
Enter an integer: 2 3
Enter a second integer: 5
似乎如果在第一次调用 nextInt() 时输入“2 3”,或者两个整数之间有一个 space,那么下次调用 nextInt() 时,它会收到首先将两个整数加在一起,然后停止程序。这里到底发生了什么?
P.S。在以更好的方式格式化我的代码并使其更有条理以供其他编码人员阅读时,是否有人对我有建议?
当您输入“2 3”(两个整数之间有一个 space)时,scanner.nextInt()
将拉入 2 而将 3 留在扫描仪中。现在,当调用下一个 nextInt()
时,它将拉入剩下的 3 个,而无需用户输入更多数据。
您可以使用 nextLine()
解决此问题,并检查输入字符串是否不包含 spaces。
像这样:
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
int a = getInt();
System.out.print("Enter a second integer: ");
int b = getInt();
int result = a + b;
System.out.println(result);
}
public static int getInt() {
while (true) {
try {
String input = sc.nextLine();
if (!input.contains(" ")) {
int integer = Integer.parseInt(input);
return integer;
} else {
throw new InputMismatchException();
}
} catch (InputMismatchException | NumberFormatException e) {
System.out.print("I'm sorry, that's not an integer. Please try again: ");
}
}
}
结果:
Enter an integer: 2 3
I'm sorry, that's not an integer. Please try again: 2
Enter a second integer: 3
5
我正在做与处理异常相关的练习。在使用 Scanner class 和以下练习检查 InputMismatchExceptions 时,我从以下代码中得到了以下结果。
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.print("Enter an integer: ");
int a = getInt();
System.out.print("Enter a second integer: ");
int b = getInt();
int result = a + b;
System.out.println(result);
}
public static int getInt(){
while (true){
try {
return sc.nextInt();
}
catch(InputMismatchException e){
System.out.print("I'm sorry, that's not an integer."
+ " Please try again: ");
sc.next();
}
}
}
输出是:
Enter an integer: 2 3
Enter a second integer: 5
似乎如果在第一次调用 nextInt() 时输入“2 3”,或者两个整数之间有一个 space,那么下次调用 nextInt() 时,它会收到首先将两个整数加在一起,然后停止程序。这里到底发生了什么?
P.S。在以更好的方式格式化我的代码并使其更有条理以供其他编码人员阅读时,是否有人对我有建议?
当您输入“2 3”(两个整数之间有一个 space)时,scanner.nextInt()
将拉入 2 而将 3 留在扫描仪中。现在,当调用下一个 nextInt()
时,它将拉入剩下的 3 个,而无需用户输入更多数据。
您可以使用 nextLine()
解决此问题,并检查输入字符串是否不包含 spaces。
像这样:
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
int a = getInt();
System.out.print("Enter a second integer: ");
int b = getInt();
int result = a + b;
System.out.println(result);
}
public static int getInt() {
while (true) {
try {
String input = sc.nextLine();
if (!input.contains(" ")) {
int integer = Integer.parseInt(input);
return integer;
} else {
throw new InputMismatchException();
}
} catch (InputMismatchException | NumberFormatException e) {
System.out.print("I'm sorry, that's not an integer. Please try again: ");
}
}
}
结果:
Enter an integer: 2 3
I'm sorry, that's not an integer. Please try again: 2
Enter a second integer: 3
5