jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Can I do this with a Switch case? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I do this with a Switch case?" Watch "Can I do this with a Switch case?" New topic
Author

Can I do this with a Switch case?

Mario Gimenez
Ranch Hand

Joined: Mar 23, 2010
Posts: 38
Hi. I need use a switch case but I don't know if this is correct.


I won't do write each case because I don't know if this ok.

thanks.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308


where num must be a type compatible with an int (byte,short,char,int) or, as java6, an enum

case 1..99: does not exist in java 6

Bye

Bye,
Nicola
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

In this case, an if-else if-else if would seem better:
With these ranges there are other tricks using % 10, % 100 and % 1000 as well:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Rob Prime wrote:... With these ranges there are other tricks using % 10, % 100 and % 1000 as well:

Ah, but 100 % 10 is 0. (As with 1000, 10000, etc.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Mario Gimenez wrote:Hi. I need use a switch case...

Do you really need to use switch/case?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343




(But we always write clean, readable code, so we would never do this in practice. Right?)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's just cruel.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
marc weber wrote:
Rob Prime wrote:... With these ranges there are other tricks using % 10, % 100 and % 1000 as well:

Ah, but 100 % 10 is 0. (As with 1000, 10000, etc.)

There's something amusing in the fact that a guy named Prime overlooked this. Maybe he's not used to numbers having other factors.

On a related note though, it's worthwhile to pay attention to the ordering of if / else if / else clauses, and avoid needless redundancy. For this example here, I'd rather code it like this:

I replaced "Hola" with three different strings A-B-C, since otherwise the whole construct is pointless. Anyway, organizing the statements this way, we don't need to specify (for example) <=99 for one group, and >=100 for the next. We eliminate needless redundancy, and also make it easier to make alterations if we need to change the cutoff from 100 to, say, 256, in a future release. This way we only need to change one number, instead of two. And we're less likely to have problems because we said < instead of <, or because we refactored num to be a double rather than an int.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

marc weber wrote:
Rob Prime wrote:... With these ranges there are other tricks using % 10, % 100 and % 1000 as well:

Ah, but 100 % 10 is 0. (As with 1000, 10000, etc.)

You are so right of course. I was so busy about trying to prevent nested ifs that this slipped by. "&& num / 10 == 0" and "&& num / 100 == 0" should fix this. Or just stick with the simple range checking.

Mike Simmons wrote:There's something amusing in the fact that a guy named Prime overlooked this.

adam smith ii
Greenhorn

Joined: Feb 04, 2010
Posts: 20
Rob Prime wrote:In this case, an if-else if-else if would seem better:
With these ranges there are other tricks using % 10, % 100 and % 1000 as well:


As well as other remarks about 0 remainder, will this code accomplish anything for any number not ending in 0? That is to say, the comparison of (num % (power of 10)) to zero will always be false using the remainder operator.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
I think that mistake has already been noticed

It might work better with / than %
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I do this with a Switch case?
 
Similar Threads
This Old Man code from Art and Science of Java
inner loop in switch
Switch statement
Dans Questions : logic
how do i loop not using a for statement?