1

Why doesn't my loop end when I enter "Done"?

    List<String> x = new ArrayList<String>();
    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    String line = "";

    while (line != "Done")
    {
        System.out.println("?> ");
        line = is.readLine();
        x.add(line);

    }
0

6 Answers 6

6

You have to use equals when comparing strings. Try this:

while(!"Done".equals(line))

!= will check the reference, not the content of the string.

Sign up to request clarification or add additional context in comments.

2 Comments

Just to elaborate. As Strings in Java are objects == will compare the values each has. "Done" is a reference to a String object. So it will only be true if both references being compared are exactly the same. == should be used to compare primitives e.g. int, char etc. Where obj1.equals(obj2) should be used to compare objects.
Just FYI, to elaborate on your answer -- if you know the strings you're comparing are both in the static string pool, you actually can use == and != on them. And you can force both strings to be in the static string pool by calling .intern(). I'm adding this note not because calling .intern() is the right thing to do (it usually isn't), but because understanding why sometimes == works on strings and sometimes it doesn't is valuable knowledge for any Java programmer.
2

Replace this:

while (line != "Done")

with this:

while (!line.equals("Done"))

You can't compare two strings for lexical inequality using !=; that only tests whether they are different objects.

Comments

1

You'll have to use equals(), == identity does not work with Strings as you might expect.

Comments

1
while (!line.equals("Done")) {
    System.out.println("?> ");
    line = is.readLine();
    x.add(line);
}

Comments

0

Try this:

import java.io.*;
import java.util.*;

class Tmp
{
  public static void main (String[] args) throws IOException
  {
    List<String> x = new ArrayList<String>();
    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    String line = "";

    while (line.compareTo ("Done") != 0)
    {
        System.out.print("?> ");
        line = is.readLine();
        x.add(line);
        System.out.println ("IO: " + line + "..."); 
    }
  }
}

The issue is that "==" compares the object reference of the string constant "Done" to the reference to the other string object "line". Since they're two different objects, their references are never equal.

Instead, you need to COMPARE the value "Done" to the value of "line".

'Hope that helps!

PS: When I saw this question, there were no replies.

It never takes me any less than 10-15 minutes to formulate and test a reply.

By which time multiple other replies inevitably squeeze in.

I don't do "fast" ;)

But I try my best for "accurate" ;)

Comments

0

You can't compare two strings using !=

You'll have to use equals() method.

while (!line.equals("Done"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.