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

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

Edited 4 Years Ago by JamesCherrill: n/a

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

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

This question has already been answered. Start a new discussion instead.