This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another switch statement query

 
Albert Richards
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have already posted concerning switch statements. I was wondering if someone would be able to tell me how to implement and output a switch statement which I have in one class, say Numbers.



And then use these case inputs in another separate class, using a scanner to take the input and output the values of the case (e.g. input 1 into scanner --> 0.1 returned)

Also, once this has been done, would it be possible to refer back to this value, and use it to add to another number which had been inputted into the scanner?

Thanks again!!
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the next number from the scanner I think there is a method like nextInt(). I'd recommend looking at the online JavaDocs for the Scanner class.

And in between each of your case statements you will want to place a break; statement. If you don't it will return every value after the first matching value. So if your case was 1, it would return 0.1, 0.2, 0.2, and 0.00.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W. Joe Smith wrote:And in between each of your case statements you will want to place a break; statement. If you don't it will return every value after the first matching value. So if your case was 1, it would return 0.1, 0.2, 0.2, and 0.00.


No it won't. The return statement will cause the method to exit at that point. If you put a break after the return you will probably get an unreachable statement compilation error.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
W. Joe Smith wrote:And in between each of your case statements you will want to place a break; statement. If you don't it will return every value after the first matching value. So if your case was 1, it would return 0.1, 0.2, 0.2, and 0.00.


No it won't. The return statement will cause the method to exit at that point. If you put a break after the return you will probably get an unreachable statement compilation error.


Teach me to try to give advice without coffee...........I didn't even notice the return in there......
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As it was pre-coffee I think we can forgive you
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it wouldn't return "every value" anyway--it would return the last one; Java has no native multiple return value capability. Without breaks (or returns) it simply "falls through" to the next case:
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but if I were to put a System.out.println() after every case, it would output all of them starting with the matching case condition, right?
 
Albert Richards
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, thanks for all the help.

Could someone explain how I could pass the output of the case statement to another class which is using a scanner to get the input of either 1, 2 ,3 (e.g, the user inputs 1 into the scanner, then the value 0.10 is returned.)




I cannot seem to work it out how to get the scanner to take the value, then return the corresponding case return. I hope I have been clear enough, Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Joe: Sure, it'd print them--but that doesn't seem to have much to do with what the code is trying to accomplish, no?
 
Albert Richards
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right, not really no. I'm sorry I can't explain myself better.

Basically, I want one of those values to be returned, so it can be added to a cost which I have already calculated.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was replying to Joe; sorry.

double foo = chooseGrade(theGrade)

Then just use foo.
 
Albert Richards
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi my bad, I just realised. Thanks for that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic