两个代码和 InputMismatchException 之间的区别

Difference between two codes and InputMismatchException

提问人:InfinityLoop 提问时间:2/9/2022 更新时间:2/9/2022 访问量:28

问:

我是 java 和类扫描仪的新手。 我有两个代码,我不明白为什么其中一个会抛出 InputMismatchException。 我期待着答案。 这里是两个代码:

第一个出现异常错误:

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    
    String firstName, lastName, completeName;
    int age;
    
    System.out.println("Please enter your first name: ");
    
    firstName = sc.nextLine();
    
    System.out.println("Please enter your last name: ");
    
    lastName = sc.nextLine();
    
    System.out.println("Please enter your complete name and your age: ");
    
    completeName = sc.next();
    age = sc.nextInt();
    
    System.out.println("Your complete name is: " + completeName);
    System.out.println("Your age is: " + age);

}

安慰:

请输入您的名字:

彼得

请输入您的姓氏:

亨利

请输入您的全名和年龄:

彼得·亨里克 22

(InputMismatchException)

第二个没有错误:

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    
    String firstName, lastName;
    int age;
    
    System.out.println("Please enter your first name: ");
    
    firstName = sc.nextLine();
    
    System.out.println("Please enter your last name: ");
    
    lastName = sc.nextLine();
    
    System.out.println("Please enter your age: ");
    
    age = sc.nextInt();
    
    System.out.println("Your complete name is: " + firstName + " " + lastName);
    System.out.println("Your age is: " + age);

}

安慰:

请输入您的名字:

彼得

请输入您的姓氏:

亨利

请输入您的年龄:

22

你的全名是:彼得·亨里克

您的年龄是:22岁

异常 java.util.scanner 差异 inputMismatchException

评论

1赞 maloomeister 2/9/2022
next()读取下一个可用令牌(最多到指定的分隔符)。默认分隔符为空格。因此,当您输入“Peter Henrik”并致电时,您只能阅读 .之后,它仍然在扫描器缓冲区中,这就是为什么您会听到 .sc.next()PeterHenrik 22InputMismatchExceptionsc.nextInt()
0赞 InfinityLoop 2/9/2022
感谢您的回答...所以我可以读取这样的新标记: completeName = sc.next() + “ ” + sc.next();

答:

0赞 maloomeister 2/9/2022 #1

看一下 Scanner#next() 的 Javadoc 会告诉你以下几点:

从此扫描程序查找并返回下一个完整令牌。完整标记的前面和后面是与分隔符模式匹配的输入。

的默认分隔符是空格。Scanner

知道这一点后,您可以提供以下输入

Peter Henrik 22

并尝试通过以下方式读取此输入

completeName = sc.next();
age = sc.nextInt();

调用将读取第一个完整的标记,直到空格,即“Peter”。 所以在缓冲区中仍然可用。sc.next()Henrik 22Scanner

因此,将读取并尝试将其解析为 .sc.nextInt()HenrikintInputMismatchException

要读取这两个令牌以获取完整名称,只需将

completeName = sc.next();

completeName = sc.next() + " " + sc.next();

但是,名称并不总是仅由两部分组成。由于可能存在两个以上单个标记的名称,因此您应该/可以按如下方式执行操作(前提是您仍想在一行中输入完整的姓名和年龄):

String line = sc.nextLine(); // read the whole line
// everything up to the last token is the name
completeName = line.substring(0, line.lastIndexOf(" "));
// last token is the age
age = line.substring(line.lastIndexOf(" ") + 1);
0赞 Vidhi Arora 2/9/2022 #2

您的第一个代码抛出 InputMismatchException,因为 sc.next() 将第一个完整标记 (“Peter”) 读取到空格(因为 blankspace 是 Java 中 Scanner 的默认分隔符),因此在此之后,sc.nextInt() 方法将读取 “Henrik”,这是一个字符串,与您期望读取年龄 (22) 不同。

您可以执行以下操作,以便读取用户的完整姓名以及年龄 [条件:您必须在新行中输入年龄]:

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String firstName, lastName,completeName;
int age;

System.out.println("Please enter your first name: ");

firstName = sc.nextLine();

System.out.println("Please enter your last name: ");

lastName = sc.nextLine();

System.out.println("Please enter your complete name: ");

completeName = sc.nextLine();

System.out.println("Please enter your age: ");

age = sc.nextInt();

System.out.println("Your complete name is: " + completeName);
System.out.println("Your age is: " + age);

}

PS:另一种解决方案可以是像您一样在一行中读取完整的名称和年龄,然后使用Java中的String split()方法拆分令牌。