aspose file tools*
The moose likes Beginning Java and the fly likes Sentinel Value for While Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sentinel Value for While Loop" Watch "Sentinel Value for While Loop" New topic
Author

Sentinel Value for While Loop

Kevin Thien
Greenhorn

Joined: Aug 12, 2012
Posts: 13
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

Joined: Oct 02, 2003
Posts: 11479
    
  16

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:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kevin Thien
Greenhorn

Joined: Aug 12, 2012
Posts: 13
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Aug 12, 2012
Posts: 13
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

Joined: Oct 02, 2003
Posts: 11479
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sentinel Value for While Loop