扫描仪问题 - Java
我试图选择读取包含多个单词的字符串,即。洛杉矶或纽约市。如果有两个单词,则使用scanner.next() 表示“出发”和“到达”只会读取第一个单词,并将它们拆分到变量之间。 nextLine() 也不太幸运。这是我的代码:
System.out.print("\nEnter flight number: ");
int flightNumber = Integer.valueOf(scanner.nextLine());
System.out.print("\nEnter departing city: ");
String departingCity = scanner.nextLine();
System.out.print("\nEnter arrival city: ");
String arrivalCity = scanner.nextLine();
我知道这很简单,但我还没有弄清楚。
以下是上面代码的输入/输出:
输入航班号:29
输入出发城市:(立即跳到下一行)
输入到达城市:
---- 我真正要去的 ----
输入航班号码:29
输入出发城市:洛杉矶(可以输入多个单词而不会跳过下一个输入)
输入到达城市:堪萨斯城
I'm trying to have the option of reading a string with multiple words, ie. Los Angeles or New York City. Using scanner.next() for "Departure" and "Arrival" would only read the first one if there were two words and split them between variables. nextLine() has not been much luck either. Here's my code:
System.out.print("\nEnter flight number: ");
int flightNumber = Integer.valueOf(scanner.nextLine());
System.out.print("\nEnter departing city: ");
String departingCity = scanner.nextLine();
System.out.print("\nEnter arrival city: ");
String arrivalCity = scanner.nextLine();
I know it's something simple but I haven't figured it out.
Here's the input/output w/ the code above:
Enter flight number: 29
Enter departing city: (immediately it skips to the next line)
Enter arrival city:
---- What I'm really going for ----
Enter flight number: 29
Enter departing City: Los Angeles (be able to type multiple words without it skipping the next input)
Enter arrival city: Kansas City
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是 next() 不读取回车符,它会被你的下一个 next() 或 nextLine() 自动读取。始终使用 nextLine() 并将输入转换为整数:
Your problem is that next() does not read the carriage return and it gets automatically read by your next next() or nextLine(). Use nextLine() all time and convert input to integer:
Integer.parseInt(scanner.nextLine())
也可以工作——它返回一个 int,而Integer.valueOf(scanner.nextLine())
返回一个整数
。作为 @Edwin Dalorzo 建议的替代方案,您可以调用
nextInt()
从输入流中获取下一个标记,并 尝试将其转换为 int。如果转换为 int 不成功,此方法将抛出InputMismatchException
。否则,它将仅获取输入的 int 值。调用nextLine()
将抓取 int 之后的行中输入的任何其他内容。此外,nextLine()
将使用用户按“enter”提交输入时添加的换行符(它将前进并丢弃它)。如果您想确保用户在按“Enter”之前没有输入除 int 之外的任何内容,请先调用
nextInt()
,然后确保 的值code>nextLine() 为空。如果您不关心 int 之后的行中输入的任何内容,则可以忽略 nextLine() 返回的内容,但仍应调用该方法来使用换行符。在 StackOverflow 中搜索“java Scanner next”或“java Scanner nextLine”以查找有关此主题的线程。
Integer.parseInt(scanner.nextLine())
would also work--it returns an int, whileInteger.valueOf(scanner.nextLine())
returns anInteger
.As an alternative to @Edwin Dalorzo's suggestion, you can call
nextInt()
to grab the next token from the input stream and try to convert it to an int. This method will throw anInputMismatchException
if conversion to an int was unsuccessful. Otherwise, it will grab only the int value entered. CallingnextLine()
, will then grab anything else that was entered in the line after the int. In addition,nextLine()
will consume the newline character added when the user pressed "enter" to submit the input (it will advance past it and discard it).If you want to be sure that the user didn't enter anything except an int before pressing "Enter," call
nextInt()
first and then make sure the value ofnextLine()
is empty. If you don't care about anything entered in the line after the int, you can ignore whatnextLine()
returns but should still call that method to consume the newline character.Search StackOverflow for "java scanner next" or "java scanner nextLine" to find threads on this subject.