• 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

Can I do this with a Switch case?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Do you really need to use switch/case?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



(But we always write clean, readable code, so we would never do this in practice. Right?)
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's just cruel.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that mistake has already been noticed

It might work better with / than %
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic