简体   繁体   English

如何在while循环中读取带空格的字符串(Java)

[英]How to read Strings with spaces inside while loop (Java)

This always gives me a headache. 这总是让我头疼。 I am trying to read and save multiple-word Strings for the fields "name" and "malady" within a while loop. 我正在尝试在while循环内读取并保存字段“ name”和“ malady”的多字字符串。 Here is my code: 这是我的代码:

while(OR1.isFull() == false || OR2.isFull() == false)
{
    // prompt for next request
    System.out.println("Enter patient info:");

    // read patient info
    System.out.print("Name: ");
    String name = input.nextLine();
    input.nextLine();
    System.out.print("Malady: ");
    String malady = input.nextLine();
    input.nextLine();
    System.out.print("Priority: ");
    int priority = input.nextInt();

    // store patient info
    Patient patient = new Patient(name, malady, priority);

    OR1.add(patient);

} // end while

System.out.println("List of patients scheduled for Operating Room 1");
while(OR1.isEmpty() == false)
{
    // pop and print root
    System.out.print(OR1.remove());
}

And here is what my console input and output looks like: 这是我的控制台输入和输出的样子:

Enter patient info: 输入患者信息:
Name: John Doe 姓名:约翰·杜
Malady: Broken hip 疯子:臀部骨折

Priority: 6 优先级:6

List of patients scheduled for Operating Room 1 预定在手术室1中使用的患者清单
Patient: Malady: Broken hip Priority: 6 患者:患病:髋部骨折优先级:6

// end console output. //结束控制台输出。

Notice that it did not record the value I entered for "Name," and also that it prompted for an extra line of input after obtaining "Malady" (required me to press enter again to get it to ask for the next input, "Priority"). 请注意,它没有记录我为“名称”输入的值,并且在获取“疾病”后它提示输入额外的一行(要求我再次按Enter才能要求输入下一个输入“优先级” ”)。

I have read the documentation at http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html . 我已经阅读了http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html上的文档。 I have tried different combinations of next() and nextLine(). 我尝试过next()和nextLine()的不同组合。 I just don't get it. 我就是不明白。

Problem solved by changing the code to the following: 通过将代码更改为以下内容解决了问题:

        // read patient info
        System.out.print("Name: ");
        input.nextLine();
        String name = input.nextLine();
        System.out.print("Malady: ");
        String malady = input.nextLine();
        System.out.print("Priority: ");
        int priority = input.nextInt();

Though I am still confused about how this silly scanner works =/ 尽管我仍然对这种傻瓜式扫描仪的工作方式感到困惑= /

I believe it should work for just: 我相信它应该只适用于:

// read patient info
System.out.println("Name: ");
String name = input.nextLine();
System.out.println("Malady: ");
String malady = input.nextLine();
System.out.println("Priority: ");
int priority = input.nextInt();

Perhaps the problem is in the Patient constructor or something? 也许问题出在Patient构造函数中?

Problem solved by changing the code to the following: 通过将代码更改为以下内容解决了问题:

    // read patient info
    System.out.print("Name: ");
    input.nextLine();
    String name = input.nextLine();
    System.out.print("Malady: ");
    String malady = input.nextLine();
    System.out.print("Priority: ");
    int priority = input.nextInt();

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

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