aspose file tools*
The moose likes Beginning Java and the fly likes Switch with Boolean ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Switch with Boolean ???" Watch "Switch with Boolean ???" New topic
Author

Switch with Boolean ???

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

"switch" works only with integral types. boolean values have their own branching control structure: the "if" statement.


[Jess in Action][AskingGoodQuestions]
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Dec 05, 2005
Posts: 134
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

Joined: Jan 30, 2006
Posts: 147
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

Joined: May 25, 2006
Posts: 34
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

Joined: Feb 02, 2006
Posts: 2023

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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Quicker way to handle FebruaryCR
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

I would do this without a switch statement at all, you can do the whole thing in two lines of code:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Sep 16, 2005
Posts: 1780
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?


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch with Boolean ???