0

Hi. So my problem is that my loop doesn't stop.

Here's my code.

public void askData ()
    {
        String word;
        String stop = null;
        int line = 0;

        File dataInput = new File ("dataIn.text");
        c.println ("In order to stop, just leave it blank.");
        c.println ();

        while (true)
        {
            try
            {
                FileWriter data = new FileWriter (dataInput);
                c.print ("Enter word " + (line + 1) + ": ");
                word = c.readString ();
                data.write (word);
                data.close ();
                line++;

                if (word.equals (stop))
                {
                    displayData ();
                }


            }
            catch (IOException e)
            {
                new Message ("Error");
            }
        }
    }

Basically what I want is that it calls displayData method when the user doesn't input anything and just press enter.


Here's the code for the dsiplayData.. It's basically for testing to see that it works.

public void displayData ()
    {
        c.print ("It works");       
    }
2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Daigan
0

If you have a while (true) you may want to consider calling break; at some point to exit the loop.

Edited by JamesCherrill: n/a

0

Yeah I tried that inside my if statement but it didn't work.

0

Oh, thanks. My if statement was incorrect. Thanks a lot.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.