• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

I honestly have no idea what to call this

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to java, and this is the first program I've been trying to code outside of the book exercises. The finished product is meant to do some calculations for a game (Kerbal Space Program if anyone's interested), and, because of the different options that affect the outcome, I decided to try making an interactive menu system using several switch statements. This is where the problem lies.

Full source code:


When the program starts, it shows the start menu fine:


You can then put in a character, hit enter, and it responds, mostly fine - putting in R/r restarts the program (in this case just redrawing that menu) and putting in H/h shows some placeholder text then restarts. Putting in 1 does give an error message I put in which it shouldn't, but that's something I haven't investigated yet, because the main issue I'm having is when you put in Q/q - while it should show a quit message, then finish the program, what actually happens depends on what's been input before that, and I have no idea why.

At first I thought it was random, but when I started making note of the number and order of the inputs I found there were some patterns (which at least implies some logic to the whole thing):

If I put in Q/q as the first input, or put in 1 (any number of times) beforehand, it quits in the way I expect and intend.

If I put in H/h or R/r (in various combinations) before trying to quit, sometimes it comes up with multiple of the quit messages, then quits, and other times it makes me put in Q/q multiple times before actually quitting, giving me various combinations the quit message and help text and restarting. Once I got to investigating this, I made these notes of my findings:



The notation I've used works like this:

The first line of each block shows the order of operations:
start is the program start, with each input following in the order given
h is help (H/h)
'r' is restart (R/r)
q is quit (Q/q)

The tabbed lines under this explain what happened with each input of Q/q:
does quit means it showed the quit message
does help means it showed the help text
quits means the program actually ended

The numbers in brackets show the number of repetitions of the preceding item.

e.g. for case start - h - 'r' (3) - q - I input h once, 'r' three times, then pressed q until the program closed, giving the outputs as follows:
First q input: showed the quit message, then showed help text and restarted (as if I'd input h)
Second q input: showed quit message, then showed help text and restarted
Third q input: show quit message twice, then showed the help text
etc

As I say, I see no reason why it would do that, and therefore how to fix it, and would really appreciate any help.

Sorry if the title isn't particularly useful - I tried to think of something relevant to the problem, but don't really know what the problem is. The same reason I haven't looked round google/ etc - I don't know what to use as search terms.

Thanks,

Chris

Edit: Attachments not working as expected, so added source code directly into post
Edit 2: Actually put in code
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

C. J. F. Jones wrote:
Edit: Attachments not working as expected, so added source code directly into post



Attachments are for images -- not for source code... and I don't think what you added to the post is source code either.

Henry
 
C. J. F. Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
Attachments are for images -- not for source code... and I don't think what you added to the post is source code either.

Henry



Fair enough.

And that's the entirety of my program, which is what I thought source code was.

Chris
 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

C. J. F. Jones wrote:

Henry Wong wrote:
Attachments are for images -- not for source code... and I don't think what you added to the post is source code either.

Henry



Fair enough.

And that's the entirety of my program, which is what I thought source code was.



Then you are in the wrong forums, because this....

C. J. F. Jones wrote:
Full source code:



is *not* Java code.

Henry
 
C. J. F. Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've changed it now.

Sorry I didn't check things properly - I'm usually better at avoiding that kind of massive mistake.

Chris
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is probably in your switch statements, but the code is long enough and you have so many switches in there I don't care to try to debug it further. I will point out what I believe the major flaw is:


See this page

Unless a case statement has a break in it, the code 'falls through' to the next case statement. so if the choice the user made was 'H', since the 'H' case doesn't have a break statement, then 'H' and 'h' would happen. And since 'h' doesn't have a break statement, then 'Q' would happen also. But 'Q' does have a break, so 'q' would not happen. So cleanup would be to collect all the cases which do the same thing and let them fall through to each other, then have a break statement to prevent falling through to the next action:
 
C. J. F. Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, that makes sense. I'll give that a go when I'm able.

Thanks,

Chris
 
C. J. F. Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve, that's sorted it.

Chris
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, glad I could be of help.
 
Rancher
Posts: 1039
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unless a case statement has a break in it, the code 'falls through' to the next case statement.



Indeed, it is a piece of the C heritage.

Fallthrough in Switch
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!