• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why don't my error message work? Using Switch cases

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I'm very new to java and find the language quite difficoult but I really wanna learn so I thought maby I could find some wisedome in this forum
Here is a small sample of a program where you register dogs for a kennel, this short sequence is the code for the menu where the user writes commands which the program then understands. Altough I want the program to be able to give the user an error message when something was done incorrectly, I thought by using Switch and Cases that putting the error message at Default would work since I thought the println in default would only be written out if the user wrote something the previus cases didn't already cover, the problem is that my program now ALWAYS writes out the println in default, even when the user does everything correctly...
Any idea what's wrong with my menu code?


 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my program now ALWAYS writes out the println in default,


Be sure to print out the value of choice in the error message so you can see why  default was called.
 
Nat Wolf
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

my program now ALWAYS writes out the println in default,


Be sure to print out the value of choice in the error message so you can see why  default was called.



I'm sorry but I don't really understand what you mean.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add the variable: choice to the print statement on line 28 so you can see its value.

 
Nat Wolf
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Add the variable: choice to the print statement on line 28 so you can see its value.




Ok, I'm not sure what you're trying to find out but now whenever the user writes the correct command this gets printed:
An error occurred, try again! choice=<

While if the user writes a command that does not excist (like the key "w " for example) this gets printed out:
An error occurred, try again! choice=w<
 
Norm Radder
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

An error occurred, try again! choice=<


That shows that the contents of choice was empty.  There was no value for the switch statement to use.
Check the code to see how choice can not have a value.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Nat Wolf
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

An error occurred, try again! choice=<


That shows that the contents of choice was empty.  There was no value for the switch statement to use.
Check the code to see how choice can not have a value.



I've been going through my code and I don't understand how "choice" cannot have a value... I'm so confused haha.



I tought line 10 "String choice = scan.nextLine();" is where "choice" gets a value.
And Thanks @Campbell Ritchie !
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you setting "scan" variable? What scan method are you calling prior to calling runCommand()?
I think we need to see more code.
 
Norm Radder
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the code call any other Scanner methods using the scan variable?  There can be empty lines left in the Scanner class's buffer that will be read as an empty String when nextLine() is called.
 
Nat Wolf
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Where are you setting "scan" variable? What scan method are you calling prior to calling runCommand()?
I think we need to see more code.



Here is more code from this class. There is actually more under this but I think that won't be necessary to see since it's code for the seperate functions you can do in the program and not directly about the menu (I'm a beginner tho so I could absolutely be wrong, just tell me if you think so!)
And thanks everyone for trying to figure this out with me, the help is much appreciated!

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not possible to execute the code for testing without having all of it.  

Can you post all the code?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm scratching my head. What you posted works for me (?).
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure the user did not press Enter before typing anything in? Pressing Enter without typing anything will return an empty line.
 
Nat Wolf
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll post everything.
This is the main class:






Here is the secound class:




Just to clarify, the problem is not that the program dosen't work, becouse it works as it should EXCEPT for the fact that the error message for typing the wrong command in the menu shows even you type the correct command. I tought putting it in Default when writing the menu with Switch and cases was correct but it's not working as it should  
 
Nat Wolf
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realise now I'm probably not describing this the best. Here's a picture of what I mean is wrong, the user writes the correct command to go to "Register new dog", then fills in everything correctly, but after this dog "bella" is registered succefully the menu error message still shows up  
And this happens when you succefully do the other commands aswell.

 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't actually enter a command after it shows you the menu and askes you to enter a command.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did the code call a Scanner nextxxx method that left a newline character in its buffer before a call to the nextLine() method? The call to nextLine will return an empty String.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the problem is caused because registerDog() uses scan.nextDouble() to read a weight, but that statement will block until you've hit enter. After you hit enter, it will read the weight from the line you entered, but the scanner won't proceed past the newline that's still in the input when you hit enter. It processes that newline when reach nextLine() in your loop, which doesn't have any characters preceding it. The solution is to call nextLine() after you've read the age from the input, and call it again after you've read the weight from the input.
 
Nat Wolf
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I believe the problem is caused because registerDog() uses scan.nextDouble() to read a weight, but that statement will block until you've hit enter. After you hit enter, it will read the weight from the line you entered, but the scanner won't proceed past the newline that's still in the input when you hit enter. It processes that newline when reach nextLine() in your loop, which doesn't have any characters preceding it. The solution is to call nextLine() after you've read the age from the input, and call it again after you've read the weight from the input.




That solved it!! I also noticed that the same problem was over at "printDogs() " with the:

And your solution fixed that too so now the program works like it should! Thanks Stephan van Hulst and everyone else for the help!  
 
reply
    Bookmark Topic Watch Topic
  • New Topic