import java.util.Scanner;
class MyClass
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int employeeId, supervisorId;
String name;
System.out.println("Enter employee ID:");
employeeId = scanner.nextInt();
System.out.println("Enter employee name:");
name = scanner.next();
System.out.println("Enter supervisor ID:");
supervisorId = scanner.nextInt();
}
}
我在尝试输入名字和姓氏时遇到了这个异常。
Enter employee ID:
101
Enter employee name:
firstname lastname
Enter supervisor ID:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at com.controller.Menu.<init>(Menu.java:61)
at com.tests.Employeetest.main(Employeetest.java:17)
但如果我只输入名字,它就可以工作。
Enter employee ID:
105
Enter employee name:
name
Enter supervisor ID:
501
我想要的是读取完整的字符串,无论它是以 asname
还是 as给出的firstname lastname
。这里有什么问题?