-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); } } } } 

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Ctrl+Z works fine when I run this program in my Eclipse, but I don't know about SPOJ environment.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.