-1

I created this small loop to test out continuous input. I want to exit when exit is the input. However the while loop I created is still looping even though exit is typed in.

code:

    String input = "";
    while(input != "exit") {
        Scanner sc = new Scanner(System.in);
        input = sc.nextLine();
    }
Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – christopher Mar 17 '14 at 00:22
  • 1
    Check this out http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – wns349 Mar 17 '14 at 00:22

2 Answers2

3

It is simple, just use input.equals("exit") instead of input != "exit"

If you use non-primitive variable (like String), you are storing reference in that variable. Comparing with == means that you are comparing if you are refering to the exactly same object.

libik
  • 22,239
  • 9
  • 44
  • 87
  • I'm not even sure we should be answering these questions any more. Surely we should just be closing them as duplicates now? – christopher Mar 17 '14 at 00:22
  • 1
    I am pretty sure we should not close them. It is arrogant to mark lower-experience question as duplicate or "bad". It is one thing I really dislike at stackoverflow community. I do not want to be a judge, I want to help people, no matter how "lame" their question is. – libik Mar 17 '14 at 00:24
  • I think the point we should consider when deciding whether to close, is whether the OP, or others like them, would have thought to Google for "string comparison" when running into this issue. It's the first thing we would do, but would someone just learning how to code know this? – merlin2011 Mar 17 '14 at 00:31
  • 1
    @merlin2011 - there is a huge problem - beginners does not know, that their issue is "string comparison". They only know that they code does not work and they does not know why. – libik Mar 17 '14 at 00:33
  • @libik is right. Sorry for such a beginner question. I did not know what my problem was. I knew that I needed to do some sort of continuous input and from my previous programming experience to compare strings was simply `!=` or `==`. I did not know of `equals()`. I was curious to why my while loop continued because to me `input !="exit"` was fine. It is hard to ask a clear straight forward question if you do not know what the specific problem is. The code for a continuous input seemed simple enough and I gave it a shot and looked up examples of continuous input. But not knowing how Java dealt – Liondancer Mar 17 '14 at 00:46
  • with string comparisons gave me trouble. – Liondancer Mar 17 '14 at 00:46
1

Change your code to

String input = "";
while(!input.equals("exit")) {
    Scanner sc = new Scanner(System.in);
    input = sc.nextLine();
}

Java string comparison uses equals to test whether two strings have the same content, while == and != is reserved for testing whether two String references refer to the same object.

merlin2011
  • 71,677
  • 44
  • 195
  • 329