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

Recommended Answers

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

Jump to Post

All 4 Replies

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

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

Try again. Add a print statement in the same if to see if that path through the code is being taken as you expected.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.