• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sentinel Value for While Loop

 
Kevin Thien
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this week in class we're working with LinkedLists and while my program runs when I use user ID as an int, my sentinel value "000" ends the program whenever an ID of "0", "00" or "000" is entered. I realised that 0 = 00 = 000, so to fix that I changed the ID from int value to string value. Now however, my code doesn't recognise any input and always outputs "No such customer".









I'm sure the issue I'm having is super simple. Also, if there's anything that I could do to fix/clean-up the code as I'm still learning Java. If possible, please explain why you do certain things so I can learn instead of just copy/pasting solutions, thanks!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shoudn't compare string (or really ANY object) with "==". You should use the .equals() method. This whole method could be simplified from this:



to this:



or better:

 
Kevin Thien
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that fixed the sentinel value issue! However, whenever I enter any other value for the ID, it's still returning a null match (presumably) as "No such customer" keeps being printed.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of nextLine() on a Scanner object. A lot of people have problems because they don’t call nextLine often enough. You may have problems if your input is a line like
1.234 5.678
You may lose the 5.678.
 
Kevin Thien
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue isn't the input not being read, but rather I think my LinkedList matching code being wrong.



All the examples we were giving were doing with int but I presumed it'd work for String as well, but that seems to not be the case, and I'm lost as to why.

EDIT: Solved it cause I'm an idiot and didn't update the customer.matches();


Thanks to the help above for the .equals() for strings!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One simple debugging technique is to put a bunch of System.out.println() statements in your code.

If you think the problem is in that method, do something like this:



You can then see EXACTLY what this method is doing...when you enter/leave it, how many times it loops, if it ever finds a match...Once you figure this out, you may learn that your matches() method doesn't return the correct value...or maybe your loop doesn't work right...but in any case, it helps you focus in on where the problem REALLY is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic