File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Another switch statement query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another switch statement query" Watch "Another switch statement query" New topic
Author

Another switch statement query

Albert Richards
Greenhorn

Joined: Apr 28, 2010
Posts: 7
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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

Joined: Aug 05, 2005
Posts: 3742
    
  16
As it was pre-coffee I think we can forgive you
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Feb 10, 2009
Posts: 710
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

Joined: Apr 28, 2010
Posts: 7
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

Joined: Sep 29, 2008
Posts: 12617

@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

Joined: Apr 28, 2010
Posts: 7
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

Joined: Sep 29, 2008
Posts: 12617

I was replying to Joe; sorry.

double foo = chooseGrade(theGrade)

Then just use foo.
Albert Richards
Greenhorn

Joined: Apr 28, 2010
Posts: 7
Hi my bad, I just realised. Thanks for that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another switch statement query