1

I have a simple program (code at bottom) that reads user input and then spits it back out. I know there maybe other solutions to my problem, but I want to use something like this because it will be useful in future programs. I am running into Stack overflow Problems. I already read this topic:

-What is a StackOverflowError? -

and I have read the top few answers, but the problem is that I want a continuous loop, because it is the purpose of the program. I was wondering if there was any way to clear my "stack" so that I could keep looping. I don't need to keep any data once I finish each run through, If that helps.

It works fine if I change line 15 from:

for(int y=0;y<System.in.available();++y)

to:

for(int y=0;y<10;++y)

but doing that puts a hard limit on the amount of characters it can read, and I don't want that. And also, if possible, I would prefer to continue using the (char)System.in.read() method of taking input.

My program looks like this:

package main;

import java.io.IOException;

public class Root 
{   
    public static void main(String[] args)
    {
        new Root();
    }

    public Root()
    {
        try{
            for(int y=0;y<System.in.available();++y)
            {
                try 
                {
                    System.out.print((char)System.in.read());
                } 
                catch (IOException exep)
                {
                    System.out.print(exep.getLocalizedMessage());
                }
            }
        }
        catch(IOException exep)
        {
            System.out.println(exep.getLocalizedMessage());
        }
        new Root();
    }

}
Community
  • 1
  • 1
Zac
  • 347
  • 3
  • 8
  • For starters, get rid of your constructor and make it a static method instead. – Bernard Nov 22 '13 at 01:49
  • Java does not support TCO and thus all recursive function calls in Java have a finite depth. Switch to using imperative loops - `while`, `for`, etc. - as appropriate. – user2864740 Nov 22 '13 at 01:49
  • 2
    If you want a loop, why not use a loop? – user2357112 Nov 22 '13 at 01:52
  • You can easily have a continuous loop. You can't have continuous recursion. (And, out of courtesy, I'm not going to tell you what your program looks like.) – Hot Licks Nov 22 '13 at 02:30
  • Im a newb. I have been programming for four years and I didn't think to use a loop :) well, I used to use perl, and then C, and I must have gotten too excited about object oriented programming. Also, the innere try/catch could be removed completely, because it is inside another try/catch that does exactly the same thing. One last thing, it helps if you keep an index variable and change the for to a while(System.in.available()!=0) because sometimes the System.in.available() does not update in time if you enter too much stuff in one press of the enter key. – Zac Nov 24 '13 at 22:22

1 Answers1

1

You seem to be creating Root objects but not really using them. The boundless recursion will keep pushing stack frames on the stack for each run of available characters, eventually ending up with a stack overflow exception -- Java doesn't do tail-call optimisation (TCO).

Why not change your Root constructor to:

public Root()
{
    while (true) {
        try{
            for(int y=0;y<System.in.available();++y)
            {
                try 
                {
                    System.out.print((char)System.in.read());
                } 
                catch (IOException exep)
                {
                    System.out.print(exep.getLocalizedMessage());
                }
            }
        }
        catch(IOException exep)
        {
            System.out.println(exep.getLocalizedMessage());
        }
    }
}
John Källén
  • 7,551
  • 31
  • 64