如何停止使用Scanner从stdin读取多行? [英] How to stop reading multiple lines from stdin using Scanner?

查看:96
本文介绍了如何停止使用Scanner从stdin读取多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理JAVA分配,应该处理多行输入.指示信息为从标准输入中读取输入".

I'm working on an JAVA assignment should process multiple lines of input. The instructions read "Input is read from stdin."

给出了示例输入示例:

one 1
two 2
three 3

我不明白上面的示例输入从stdin读取"是什么意思.

I don't understand what the above sample input "read from stdin" means.

这是我编写的一个测试程序,可以消除我的困惑:

Here's a test program I wrote that isolates my confusion:

import java.io.*;
import java.util.Scanner;

class Test
{
public static void main(String[] args)
{   
    Scanner stdin = new Scanner(System.in);
    while(stdin.hasNextLine())
    {
        String line = stdin.nextLine();
        String[] tokens = line.split(" ");
        System.out.println(Integer.parseInt(tokens[1]));
    }
}

当我在控制台中运行该程序时,它会等待我的输入,并且每当我输入一行时,它就会像预期的那样回显它.因此,我认为也许可以通过以这种方式输入3条线中的每条线来实现上面的示例输入.但是,似乎没有办法结束该过程.输入3行后,如何终止输入?我尝试只按两次Enter键,但这似乎读为仅包含换行符的行,这会导致错误,因为该行不符合预期的2个标记格式.

When I run this program in the console, it waits for my input and each time I input a line it echos it back as I would expect. So I thought perhaps the sample input above would be achieved by entering each of the 3 lines in this fashion. However, there seems to be no way to end the process. After I enter the 3 lines, how do I terminate the input? I tried just pressing enter twice, but that seems to read as a line consisting of only the newline character, which causes an error because the line doesn't fit the 2 token format it expects.

这是控制台交互的样子:

Here's what the console interaction looks like:

javac Test.java
java Test
one 1
1
two 2
2
three 3
3

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Test.main(Test.java:13)

如果能帮助我指出我的理解方面的空白,我将不胜感激.

I'd appreciate any help in pointing out the gap in my understanding.

推荐答案

您可以尝试要求输入空值

import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {   
        String line;
        Scanner stdin = new Scanner(System.in);
        while(stdin.hasNextLine() && !( line = stdin.nextLine() ).equals( "" ))
        {
            String[] tokens = line.split(" ");
            System.out.println(Integer.parseInt(tokens[1]));
        }
        stdin.close();
    }
}

  • 您的代码即将完成.您要做的就是退出while循环.在此代码示例中,我向其添加了一个条件,该条件首先将读取的输入值设置为line,然后检查返回的String是否为空.如果是,则while循环的第二个条件返回false并使其停止.
  • 仅在不输入两个值(由空格分隔)的最小值时,才会获得数组索引超出范围的异常.如果您不尝试获取第二个值,则> token [1]<通过静态索引可以避免此错误.
  • 使用阅读器时,请记住在使用阅读器后将其关闭.
  • 最后但并非最不重要的-您是否尝试过使用通常的Ctrl + C热键终止控制台中的进程?
    • Your code is almost completed. All that you have to do is to exit the while loop. In this code sample I added a condition to it that first sets the read input value to line and secondly checks the returned String if it is empty; if so the second condition of the while loop returns false and let it stop.
    • The array index out of bounds exception you will only get when you're not entering a minimum of two values, delimitted by whitespace. If you wouldn't try to get the second value >token[1]< by a static index you could avoid this error.
    • When you're using readers, keep in mind to close after using them.
    • Last but not least - have you tried the usual Ctrl+C hotkey to terminate processes in consoles?
    • 祝你好运!

      这篇关于如何停止使用Scanner从stdin读取多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆