-1

I can't believe I can't see what I'm doing wrong as this is something a beginner like me should already be used to...

I have the following code:

boolean doWhile = true;
while (doWhile == true){
     String s = sc.nextLine();
     if (s == "something"){
          doWhile = false;
     }
     //DoStuff
}

Unfortunately, when s == "something", it doesn't break the loop, but instead continues to read the rest of the code.

I've searched around and a lot of people seem to make this mistake, but in their cases they messed up with the = and == things.

I'm pretty sure = is for assigning and == to check equality. I also tried while(doWhile) and while(!doWhile) with the boolean to false, but nothing seems to work and I can't seem to wrap my head around something so simple.

EDIT: .equals() doesn't work either. Also, I've used == in this way before and it worked without a problem. Not sure why it's making a fuss right now. EDIT3: No solution here: How do I compare strings in Java? The solution was provided by harold.

Community
  • 1
  • 1
Doombringer
  • 596
  • 4
  • 19
  • 2
    `==` is for comparing primitives, and *reference equality* of objects. For *value equality* use `equals()`. – azurefrog May 11 '17 at 17:03
  • 1
    FWIW, breaking the loop can be done with the similarly-named "break". Of course you'd still have to fix your comparison. – harold May 11 '17 at 17:04
  • @harold I'd like to avoid using `break`. – Doombringer May 11 '17 at 17:07
  • 2
    Sure, if you want. Then you should realize that when you make `doWhile` false `//DoStuff` will still happen once. You could use `continue` to skip it. – harold May 11 '17 at 17:18
  • Thank you, kindly. This worked. ^^ – Doombringer May 11 '17 at 17:21
  • `while(doWhile == true)` is extremely poor practice and will make people question your comprehension and competentcy. `while(doWhile)` is the correct form. Also `doWhile` is a terrible variable name, `continueProcessing` or something provides some semantic meaning shows you know wha tyou are doing. Also there is absolutely no valid reason for *avoiding the use of the `break` keyword*. –  May 11 '17 at 17:37
  • The more correct way would be `while(!s.equals("something"))` that eliminates the hacky boolean flag variable and all the mistakes you are making because of it. –  May 11 '17 at 17:40
  • @JarrodRoberson Obviously I only name the variable like that for the question, it's not called like that in my code and there's a perfectly good reason to NOT use "break", for example, not being allowed to, like in my case + if the condition is done properly, you don't need a "break". – Doombringer May 11 '17 at 17:57

1 Answers1

0

Try this:

if (s.equals("something")){
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • I tried this but it doesn't seem to work. – Doombringer May 11 '17 at 17:18
  • Have you tried to debug the values you're testing with `System.out.println(variable);`? It can help to know what's hapening. For example: `System.out.println(s);` or `System.out.println(doWhile);` or `System.out.println(doWhile == true);`. Maybe there is something wrong the way you are testing... – Enrique de Miguel May 12 '17 at 07:20
  • I tried printing and everything seemed fine, or at least, it showed what it was supposed to for the examples I input-ed. I got it to work yesterday, anyway. Sometimes, I still have to use ".equals()", but not always. It's weird. – Doombringer May 12 '17 at 12:10
  • It is wierd because `s == "something"` shouldn't work. The last thing is some problem with your IDE – Enrique de Miguel May 12 '17 at 12:16
  • I'm using IntelliJ. It has never given me a problem before, though. – Doombringer May 12 '17 at 13:30