Java程序无法正确执行nextLine()
当我运行程序而不是读取字符串并将其存储在tempAddress中时,我的程序只是在输入前打印下一行。在前两个词中使用next是可行的,因为我只使用了一个词,但第三个词包含了多个词,因此需要其他词。通过我的研究,我发现nextLine()是答案,但我无法像其他人那样使用它,提前感谢Java程序无法正确执行nextLine(),java,Java,当我运行程序而不是读取字符串并将其存储在tempAddress中时,我的程序只是在输入前打印下一行。在前两个词中使用next是可行的,因为我只使用了一个词,但第三个词包含了多个词,因此需要其他词。通过我的研究,我发现nextLine()是答案,但我无法像其他人那样使用它,提前感谢 System.out.println("Enter Employee First Name: "); String tempFirstName = input.next(); employeesArray[i].set
System.out.println("Enter Employee First Name: ");
String tempFirstName = input.next();
employeesArray[i].setFirstName(tempFirstName);
System.out.println("Enter Employee Last Name: ");
String tempLastName = input.next();
employeesArray[i].setLastName(tempLastName);
System.out.println("Enter Employee Address: ");
String tempAddress = input.nextLine();
employeesArray[i].setAddress(tempAddress);
System.out.println("Enter Employee Title: ");
String tempTitle = input.next();
employeesArray[i].setTitle(tempTitle);
基本上,扫描器默认使用空白标记输入。使用scanner的next()方法返回空格之前的第一个标记,指针留在那里。使用nextLine()返回整行,然后将指针移动到下一行
System.out.println("Enter Employee First Name: ");
String tempFirstName = input.next();
employeesArray[i].setFirstName(tempFirstName);
System.out.println("Enter Employee Last Name: ");
String tempLastName = input.next();
employeesArray[i].setLastName(tempLastName);
//feed this to move the scanner to next line
input.nextLine();
System.out.println("Enter Employee Address: ");
String tempAddress = input.nextLine();
employeesArray[i].setAddress(tempAddress);
System.out.println("Enter Employee Title: ");
String tempTitle = input.next();
employeesArray[i].setTitle(tempTitle);
nextLine()表现不好的原因是,您以前使用next()输入员工姓氏会导致指针停留在该行中。因此,当您使用nextLine()到达获取员工地址的点时,指针将返回上一次输入next()的剩余部分,该部分显然为空(当为next()提供一个单词作为输入时)。假设您输入了两个单词,并用空格分隔姓氏,则next()将第一个单词存储在姓氏字段中,指针在第一个标记之后等待第二个标记,并在您到达nextLine()时立即返回第二个标记并移动到新行
解决方案是在读取姓氏输入后执行nextLine(),以确保指针在新行中等待地址输入
我通过在其中插入input.nextLine()更新了代码,以确保已使用扫描仪输入,并将指针移动到下一行
System.out.println("Enter Employee First Name: ");
String tempFirstName = input.next();
employeesArray[i].setFirstName(tempFirstName);
System.out.println("Enter Employee Last Name: ");
String tempLastName = input.next();
employeesArray[i].setLastName(tempLastName);
//feed this to move the scanner to next line
input.nextLine();
System.out.println("Enter Employee Address: ");
String tempAddress = input.nextLine();
employeesArray[i].setAddress(tempAddress);
System.out.println("Enter Employee Title: ");
String tempTitle = input.next();
employeesArray[i].setTitle(tempTitle);
当您有
input.next()
时,它读取输入,但不读取newline
字符,它将其保留在输入流中。input.nextLine()
以newline
字符结尾。因此,当执行input.nextLine()
时,它停止而不接受任何输入,因为它已经获得了newline
输入流中的(\n
)字符
解决方案:在执行inopt.nextLine()
之前,请阅读换行符:
另请参见:您希望nextLine()
做什么,以及您发现“无法使其工作”?希望它读取用户的输入…如上所述“我的程序只需在输入前打印下一行”此代码对我有效System.out.println(“输入员工地址:”)“;String tempAddress=input.nextLine();System.out.println(“tempAddress:+tempAddress”);
将解释它到底是什么以及它是如何不适合您的。该方法正在完成它的工作请添加一些解释。这是可行的,但如果您不想拥有“”(空格),该怎么办在您的输入中?您必须为此做一整件事。我已更新了答案,并用解决其问题的修复程序更新了代码。@789和tungd为指出问题而欢呼。@789是的,前提是姓氏始终是一个标记-这是自然现象。nextLine()不管怎样,我都会把它移到新的一行。@user2383106很高兴它能帮上忙。请投票表决这个问题,因为它很有用,而且我从回答它中学到了一些东西;)谢谢你的帮助,我已经接受了其他答案,因为它是第一个答案,但一定要投票表决你的答案和评论