简体   繁体   English

我如何在SPOJ上的Java程序中读取输入直到EOF为止?

[英]How do I read input until EOF in Java for this program on SPOJ?

Problem URL: http://www.spoj.com/problems/PT07H/ 问题网址: http//www.spoj.com/problems/PT07H/

By the way, I tried to read an input by 3 different methods but no success, control never come out of while() loop (ie, It never detects EOF or EOS). 顺便说一句,我试图通过3种不同的方法读取输入,但是没有成功,控制永远不会退出while()循环(即,它永远不会检测到EOF或EOS)。

Here's my code that reads an input XML: 这是读取输入XML的代码:

public static void main(String[] args) throws Exception {

        // Input & Output
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        OutputStream out = new BufferedOutputStream(System.out);

        int data;
        char c;
        String line;
        StringBuilder xml = new StringBuilder();

        // Read input
/*
        // Method-1
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            line = in.nextLine();
            for (int i = 0; i < line.length(); i++) {
                c = line.charAt(i);
                if (isValid(c)) {
                    xml.append(c);
                }
            }
        }
        // Method-2
        while ((data = br.read()) != -1) {
            c = (char) data;
            if (isValid(c)) {
                xml.append(c);
            }
        }
*/
        // Method-3
        while ((line = br.readLine()) != null) {
            for (int i = 0; i < line.length(); i++) {
                c = line.charAt(i);
                if (isValid(c)) {
                    xml.append(c);
                }
            }
        }

}

Using Ctrl+D and Ctrl+Z is a good one, but if you need EOF within stdin we can implement EOF as a delimiter. 使用Ctrl + D和Ctrl + Z是一个不错的选择,但是如果您在stdin中需要EOF,我们可以将EOF实现为定界符。

        Scanner sin = new Scanner(System.in);
        CharSequence end1 = "end-of-file";
        CharSequence end2 = "EOF";
        CharSequence end3 = "End Of File";
        ArrayList<String> Arr = new ArrayList<String>();
        int i =0;

        while(sin.hasNext())
        {
            Arr.add(sin.nextLine());
                              //If the input string contains end-of-file or EOF or End Of File. 
            if( (Arr.get(i).contains(end1)) == true || (Arr.get(i).contains(end2)) == true || (Arr.get(i).contains(end3) ) == true )
            {
                i++;
                break;
            }
            i++;
        }

Hope it would help someone looking for stdin End-of-file. 希望它对寻找stdin文件结尾的人有所帮助。

Standard input actually has no EOF, so you'll never see it. 标配输入居然没有 EOF,所以你永远不会看到它。 Your method #3 will work when reading from an actual file (or other stream) which does have an end. 当您从有结尾的实际文件(或其他流)中读取内容时,您的方法#3将起作用。

(You may want to try to 'type' an EOF on the stdin (Ctrl-D or Ctrl-Z), which may or may not work, not sure.) (您可能想尝试在标准输入(Ctrl-D或Ctrl-Z)上“键入” EOF,这可能会或可能不会起作用,不确定。)

See also this question. 另请参阅问题。

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

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