• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with switch  RSS feed

 
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a tutorial on programmr.com and came across a problem I can not figure out.

The problem is as follows
Given a int variable named yesCount and another int variable named noCount and an int variable named response that stores an input, write the necessary code to carry out the following:

if the value typed in is a 1 or a 2 then increment yesCount and assign "YES WAS RECORDED" to state variable.
if the value typed in is a 3 or an 4 then increment noCount and assign "NO WAS RECORDED" to state variable.
otherwise leave yesCount and noCount as they are and assign "INVALID" to state variable.


The Code I have:


But for some reason I get hung up as soon as I enter the first number. Any suggestions as to why this is happening?
(note, this is a tutorial on using switch statements so other options are not usable for the purpose of the class.)
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:But for some reason I get hung up as soon as I enter the first number.

What exactly do you mean by "I get hung up..."?

Also, I don't think the requirements ask you to do what you're doing on lines 15 and 17. The only input you're supposed to get from the user goes into response. As you've written it, you're also asking the user to enter values that will go into yesCount and noCount.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By hung up I mean it just stops at that point. It does not ask me for the initial numbers of Yes and No votes.

I do not have control over those lines (they are pre-entered)

Here is the code where we have to fill in the switch code (if that makes sense)
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:I do not have control over those lines (they are pre-entered)

Then you have a bit of a dilemma because that is where the problem is. The Scanner#nextInt method returns the value of the next token in the stream if it is an int. However it does not read the Enter key from the stream. Therefore the next time you call the nextInt method, there will be no int there for it to read - just the Enter/Return key and that will stay the same unless you call nextLine after each call to nextInt. Or you could just call nextLine instead of nextInt and use Integer.parseInt to convert to an int.
Either way, you're going to have to change that code.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay forgive my noobness... but I am not sure how to do a integer.partInt. I have tried adding a scanner.nextLine(); after line 14 however it still just stops. I did determine that the program seems to just end and not get hung up. Almost like it sees the response=scanner.nextInt(); line, does the switch, and then just ends.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just as an FYI for when you do get this sorted - if two cases in a switch statement have the same code, you can combine them
can be written as

and
can be written as
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Ignore everything I said above about Scanner.nextInt. I was probably confusing it with something else. I don't use Scanner much but remember seeing some similar problems here on coderanch.

Anyhow, I've just run your code and it works fine for me(apart from the slight logic error on lines 30 and 34), so the problem is not with the code. How are you running the program ?
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, updated my code on my machine to reflect that.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running it in eclipse. I will try with running from command line.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ran it from command line works like a charm. Thanks Joanne
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:Ran it from command line works like a charm. Thanks Joanne

Great. But it should run in Eclipse as well. I can get it to work in either.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!