扫描仪问题 - Java

发布于 2024-11-03 14:15:19 字数 702 浏览 5 评论 0原文

我试图选择读取包含多个单词的字符串,即。洛杉矶或纽约市。如果有两个单词,则使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-11-10 14:15:19

你的问题是 next() 不读取回车符,它会被你的下一个 next() 或 nextLine() 自动读取。始终使用 nextLine() 并将输入转换为整数:

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);
    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();

}

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:

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);
    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();

}
风流物 2024-11-10 14:15:19

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, while Integer.valueOf(scanner.nextLine()) returns an Integer.

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 an InputMismatchException if conversion to an int was unsuccessful. Otherwise, it will grab only the int value entered. Calling nextLine(), 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 of nextLine() is empty. If you don't care about anything entered in the line after the int, you can ignore what nextLine() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文