Forums Register Login

Sentinel Value for While Loop

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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:

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5957 times.
Similar Threads
SubClass Blues!
Transfer Function on the verge of working
Stumped, need inspiration.
Very Confused!!
not getting what this servlet is doing+MVC
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:39:19.