简体   繁体   English

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

[英]How to read strings from a Scanner in a Java console application?

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();
    }
}

I got this exception while trying to enter a first name and last name. 我在尝试输入名字和姓氏时遇到了这个异常。

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)

but its working on if I only enter the first name. 但如果我只输入名字,它的工作。

Enter employee ID:
105
Enter employee name:
name
Enter supervisor ID:
501

What I want is to read the full string whether it is given as name or as firstname lastname . 我想要的是读取完整字符串,无论是name还是firstname lastname What's the problem here? 这有什么问题?

Scanner scanner = new Scanner(System.in);
int employeeId, supervisorId;
String name;
System.out.println("Enter employee ID:");
employeeId = scanner.nextInt();
scanner.nextLine(); //This is needed to pick up the new line
System.out.println("Enter employee name:");
name = scanner.nextLine();
System.out.println("Enter supervisor ID:");
supervisorId = scanner.nextInt();

Calling nextInt() was a problem as it didn't pick up the new line (when you hit enter). 调用nextInt()是一个问题,因为它没有拾取新行(当你按Enter键时)。 So, calling scanner.nextLine() after that does the work. 因此,在此之后调用scanner.nextLine()可以完成工作。

What you can do is use delimeter as new line. 你可以做的是使用deimeter作为新行。 Till you press enter key you will be able to read it as string. 直到你按回车键,你就可以把它读成字符串。

Scanner sc = new Scanner(System.in);
sc.useDelimiter(System.getProperty("line.separator"));

Hope this helps. 希望这可以帮助。

Replace: 更换:

System.out.println("Enter EmployeeName:");
                 ename=(scanner.next());

with: 有:

System.out.println("Enter EmployeeName:");
                 ename=(scanner.nextLine());

This is because next() grabs only the next token, and the space acts as a delimiter between the tokens. 这是因为next()只抓取下一个标记,并且空格充当标记之间的分隔符。 By this, I mean that the scanner reads the input: "firstname lastname" as two separate tokens. 通过这个,我的意思是扫描程序读取输入:“firstname lastname”作为两个单独的标记。 So in your example, ename would be set to firstname and the scanner is attempting to set the supervisorId to lastname 因此,在您的示例中,ename将设置为firstname,扫描程序正在尝试将supervisorId设置为lastname

You are entering a null value to nextInt, it will fail if you give a null value... 您正在为nextInt输入一个空值,如果您给出一个空值,它将失败...

i have added a null check to the piece of code 我在这段代码中添加了一个空检查

Try this code: 试试这段代码:

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:");
                    if(scanner.nextLine()!=null&&scanner.nextLine()!=""){//null check
                     sid=scanner.nextInt();
                     }//null check
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM