Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While loop

 
Atah Tabotnjap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please help debug this piece of code. The statement block when the while expresssion resolves to true is never executed, no matter what i type in:
 
Stephan van Hulst
Bartender
Pie
Posts: 6113
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do you know it evaluates to true?
 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atah Tabotnjap wrote:Can someone please help debug this piece of code.

Actually, that's your job. What we're happy to do is point you in the right direction, and my first one is that, when you're debugging, System.out.println() is your friend.

So, what about adding:
System.out.println(choice==option);
immediately before your 'while' loop for starters?

That might give you some interesting information.

Winston

PS: Please UseCodeTags (←click) when you post code. I've added them for you this time - see how much better it looks? And please read the link carefully, because your indenting is a bit excessive.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not use == to compare strings. Use .equals(...) instead.

The == operator compares object references. The result of comparing two references will only be true when the two references on the left and right side of the == refer to the exact same object. If you have two String objects with the same content, then the result will be false, because you have to distinct String objects.

So, you'll need to use:

Note that you will probably have to add null checks as well, because if 'choice' is 'null' you will get a NullPointerException if you try to call a method such as equals on it.
 
Atah Tabotnjap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand why it is not working. When i try to print both String variables as follows:



i get:

hit
hit
false.

hit and hit are equal, while does it then evaluate to false.
 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atah Tabotnjap wrote:hit and hit are equal, while does it then evaluate to false.

Because you haven't read the AvoidTheEqualityOperator page.

'hit' and 'hit' (or rather, two String objects containing those characters) are equal(), but they're NOT necessarily '=='.

Winston
 
Atah Tabotnjap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks man !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic