• 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

Help with switch

 
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, updated my code on my machine to reflect that.
 
John Morgan
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am running it in eclipse. I will try with running from command line.
 
John Morgan
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic