title: Java Program Output (Stacks)
body: <p>This is my first question here on SO.
  It might be a stupid question, but so far I haven't been able to figure it out.</p>
  
  <p>Consider the program Reader.java below:</p>
  
  <pre><code>   public class Reader {
  
        public static void main(String [ ] args) {
  
        Stack&lt;String&gt; stack = new Stack &lt;String&gt;();
  
           while (!StdIn.isEmpty ()) {
  
              stack.push(StdIn.readString());
           }
  
           while (!stack.isEmpty()) {
  
              StdOut.print(stack.pop() + " ");
           }
           StdOut.println();
       }
     }
  </code></pre>
  
  <p>Then it is given that a text file (words.txt) has the following contents:</p>
  
  <pre><code>read
  cannot
  I
  </code></pre>
  
  <p>Given that the program has compiled successfully using the files StdIn.java, StdOut.java and
  Stack.java as used in the course, what output is produced by the following command line instruction?</p>
  
  <pre><code>java Reader &lt; words.txt
  </code></pre>
  
  <p>The solution says that the output should be:</p>
  
  <pre><code>"I can read"
  </code></pre>
  
  <p>and not</p>
  
  <pre><code>"I cannot read"
  </code></pre>
  
  <p>Why is this?</p>
  
  <p>Thanks :)</p>