There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:There's a bit too much code in your main() method for my taste, but putting that aside...
whenever the code is not doing what you think it should, especially when you are learning the language, "System.out.printnl()" is your best friend. If your code isn't following the switch branch you think it should, print out your option value just before you enter said switch. Prove to yourself it is what you think it is, and find out if it's not.
Campbell Ritchie wrote:I deleted part of my answer because Fred and JJ have already answered. Please follow their suggestions and see what you come up with. We think it is better to let you find the problem, and I had let the cat out of the bag a bit too early. I think the switch can be tidied up, but the problem you are seeing isn't in the switch.
Carey Brown wrote:A potential problem is that you are creating a new Scanner( System.in ) more than once during the execution of your code. You may get away with this or it may hiccup. The proper way is to only do this once FOR THE ENTIRE PROJECT and for THE ENTIRE TIME IT IS RUNNING.
The easiest way to do this is by making 'keyboard' a static final constant and then using that everywhere. Note that constants are, by convention, in all upper case. Also, never ever close KEYBOARD even if your IDE complains about it.
Which text? You already know about System.out.println. Use it to print option, as Fred suggested.Matthew Stein wrote:. . . just working through a text . . .
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|