Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch with Boolean ???

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to write a switch statement which will check the return statements from 3 called up methods ("february", "century", and "leap")...and based on the boolean values they return to "daysInMonth" certain commands are to be carried out......

is ist possible to use the code segment below the way i have done?
if not, what corrections could i make,....any suggestions?


 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"switch" works only with integral types. boolean values have their own branching control structure: the "if" statement.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm,
I'm a bit confused here:

because i was made to understand that if you had several "if statements" following each other you could put them all together into a switch control structure?

so if i have 20 possibilities i would have to put these all into "if statements"?

tahnx!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Booleans have just two values: true and false. Therefore a "boolean switch" could never have more than two branches. To have more than two branches, you'd actually need more than one boolean value -- i.e., you're testing more than one thing. A chain of if... else if... else statements lets you do just that.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay,
this is what I've decided to do.....how about it?



[ May 28, 2006: Message edited by: Wolfgang Obi ]
[ May 28, 2006: Message edited by: Wolfgang Obi ]
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wolf-
Just a little switch tip.
If you're going to take the same action for several cases, it is not necessary to explicitly code the action for each case. Just arrange your cases appropriatley.
The following will have the same affect as the switch you coded above.
 
Chris Wox
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by S Root:
Wolf-
Just a little switch tip.
If you're going to take the same action for several cases, it is not necessary to explicitly code the action for each case. Just arrange your cases appropriatley.
The following will have the same affect as the switch you coded above.


why don't we have a break after the If condition below
if (((isSchalt == true)&&(!(isCent)))||(isLeapCent == true)) days = dayGroup[1];


will it not become a fall through case ???
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is
.

It is good programming style to always write the curly braces, {}, althought they are not needed if the clause contains only a single statement. The curly braces are a general indicator in Java of a compound statement. This is known as a block of code. (See here)
[ May 29, 2006: Message edited by: wise owen ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quicker way to handle FebruaryCR
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do this without a switch statement at all, you can do the whole thing in two lines of code:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
I would do this without a switch statement at all, you can do the whole thing in two lines of code:


Well, except for the whole leap year thing...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a job for java.util.Calendar. I whipped up some code, but I'm puzzled by its output:

Output:

2006-1 = 31
2006-2 = 31
2006-3 = 31
2006-4 = 30
2006-5 = 31
2006-6 = 30
2006-7 = 31
2006-8 = 31
2006-9 = 30
2006-10 = 31
2006-11 = 30
2006-12 = 31

What's up with February?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic