如何从控制台应用程序JAVA中的扫描仪读取字符串?

前端之家收集整理的这篇文章主要介绍了如何从控制台应用程序JAVA中的扫描仪读取字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.util.Scanner;
class MyClass
{
     public static void main(String args[]){

                Scanner scanner = new Scanner(System.in);
                int eid,sid;
                String ename;
                System.out.println("Enter Employeeid:");
                     eid=(scanner.nextInt());
                System.out.println("Enter EmployeeName:");
                     ename=(scanner.next());
                System.out.println("Enter SupervisiorId:");
                      sid=(scanner.nextInt());  
        }
}

尝试插入这个时,我得到了输出

Enter Employeeid:
101
Enter EmployeeName:
firstname lastname
Enter SupervisiorId:
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 Employeeid:
105
Enter EmployeeName:
name
Enter SupervisiorId:
501

我想要的是,我必须读取完整的字符串,无论是作为名称还是名字lastname..Eg

猜你在找的Java相关文章