-1

Problem URL: 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 (i.e., It never detects EOF or EOS).

Here's my code that reads an input 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);
                }
            }
        }

}
Hitesh Dholaria
  • 157
  • 3
  • 13

2 Answers2

2

Using Ctrl+D and Ctrl+Z is a good one, but if you need EOF within stdin we can implement EOF as a delimiter.

        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.

Giri
  • 2,704
  • 2
  • 25
  • 27
1

Standard input actually has no EOF, so you'll never see it. Your method #3 will work when reading from an actual file (or other stream) which does have an end.

(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.)

See also this question.

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • Ctrl+Z works fine when I run this program in my Eclipse, but I don't know about SPOJ environment. – Hitesh Dholaria Sep 05 '14 at 10:25
  • Given the definition of possible input from the problem description, you do not really need an `EOF`. The input is complete as soon as the end tag of the second XML document is read. – JimmyB Sep 05 '14 at 10:48