-1

I'm trying to modify the CharStack class (below) in Eclipse, but I'm unable to get it to do anything in the console when I run it, though it works fine when compiled with javac.exe

Is this something to do with carriage returns, perhaps? How do I signal to Eclipse that I'm ready for it to accept the input and return a result (the -1)?

The algorithm for the class is just:

while (there is a character to read)     
    push the character onto a stack;

while (there are characters on the stack)     
    pop off a character and print it; 

The concept of a stack is a perfect candidate for becoming a class. It has a well-defined interface (push() and pop()) and some rules to enforce (you can only take data from the top, you can only remove as many elements as you insert). Here is a simple implementation of a stack that holds characters:

import java.io.*;
public class CharStack
{
  private char[] m_data;          

  private int m_ptr;

  public CharStack(int size)
  {
      m_ptr = 0;                  

      m_data = new char[(size > 1 ? size : 10)]; 
  }

  public void push(char c)
  {
      if (m_ptr >= m_data.length) 
      {
         // Grow the array automatically
         char[] tmp = 
            new char[m_data.length * 2];

         System.arraycopy(m_data, 0, 
                          tmp, 0, 
                          m_data.length);
         m_data = tmp;
      }
      m_data[m_ptr++] = c;
  }

  public char pop()              
  {
      return m_data[--m_ptr];
  }
  public boolean hasMoreElements()
  {
      return (m_ptr != 0);
  }


  public static void main(String[] argv) 
      throws IOException
  {
      CharStack s = new CharStack(10);
      int i;
      while ( (i = System.in.read()) != -1 )
      {
         s.push((char) i);
      }
      while (s.hasMoreElements())
      {
         System.out.write(s.pop());
      }
      System.out.println();
    }
}

When I run this in Windows CLI I get:

C:\user>java CharStack
12345

54321
  • 3
    _I'm unable to get it to do anything in the console when I run it_ What did you want it to do? What did you expect to happen? Why did you expect that? What does it actually do? – Sotirios Delimanolis Oct 19 '15 at 16:10
  • How do you pass -1 to System.in.read()? By entering Ctrl+C? – Brian Oct 19 '15 at 16:16
  • @Iyen I don't know; this is written by a prof at Berkeley, not me. Ctrl+C doesn't work in the Eclipse console. Pressing enter works with the Windows CLI. –  Oct 19 '15 at 16:41
  • 1
    @SotiriosDelimanolis I expect it to return something. It doesn't return anything. The specifics are kind of irrelevant, but I'll update the post. –  Oct 19 '15 at 16:42

1 Answers1

1

What you're looking for is the EOF character, represented OS-agnostically in this Java class.

In Windows that would be Ctrl-Z. Presumably this is implicit when you're on the Windows CLI version, but not so in Eclipse.

  • `CTRL-D` under linux, see also http://stackoverflow.com/questions/3061135/can-we-write-an-eof-character-ourselves –  Oct 19 '15 at 16:56