'Switch statement with boolean is not working?
Why is my compiler telling me:
Incompatible Types:
Required: Boolean
Found: Int
under case 0 & case 1
For instance:
public void test(boolean isOn){
switch (isOn){
case 0:
if (isOn){
System.out.println("its on");
}
break;
case 1:
if (!isOn){
System.out.println("its off");
}
break;
default:
System.out.println("I don't know!");
}
}
Driver Class:
Club me = new Club();
me.test(true);
Solution 1:[1]
You are switching on boolean
type, and your cases are using int
types. But even though you change your cases to have boolean
types, that wouldn't work. You cannot switch on boolean
type. And that wouldn't make any sense as using an if-else
would be easier anyways:
if (isOn) {
System.out.println("its on");
} else {
System.out.println("its off");
}
Note that there is no "I don't know!"
case here. A boolean
type can have either true
or false
value. This is another reason, why switch-case
is not for boolean
type. There is no default case.
You can also condense it to a single statement by using a conditional expression:
public void test(boolean isOn) {
System.out.println(isOn ? "its on" : "its off");
}
Solution 2:[2]
Just convert the boolean to number of 1 and 0.
public void test(boolean isOn){
int trueOrFalse;
if(isOn == true){
trueOrFalse = 1;
}else{
trueOrFalse = 0;
}
switch (trueOrFalse){
case 1:
if (isOn){
System.out.println("its on");
}
break;
case 0:
if (!isOn){
System.out.println("its off");
}
break;
default:
System.out.println("I don't know!");
}
}
Solution 3:[3]
switch (isOn)
: switching boolean
and want to case with int
e.g., case 0
!
According to the JLS section 14.11: for a switch ( Expression ) SwitchBlock
:
Expression can only be
char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type
other wise a compile-time error occurs.According to the specification followings are also must be true:
- Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression.
- No two of the
case
constant expressions associated with a switch statement may have the same value.- No
switch
label isnull
.- At most one default label may be associated with the same switch statement
Hence, switch expression can't be float, double or boolean
. TO answer the question why?: boolean true false
are meaningful using with if-else
, e.g., if(true) then do
. Floating point numbers (float
, double
) are not a good candiadtes for switch as exact comparison is often broken by rounding errors. e.g. 0.11 - 0.1 == 0.01
is false.
Solution 4:[4]
In Java, Switch does NOT work with Boolean.
Accepted variable types are : char,byte,short,int,String,Character,Byte,Short,Integer,enum
Solution 5:[5]
¯\_(?)_/¯
switch (Boolean.hashCode(isOn)) {
case 1231: System.out.println("its on"); break;
case 1237: System.out.println("its off"); break;
}
Solution 6:[6]
As the error clearly states, numbers are not booleans.
You want true
and false
.
Solution 7:[7]
This is not C++ where 1 and 0 get implicitly converted to/from true and false. Switching on boolean is also a waste of time even if you could do it; just write a simple if/else statement, or use the ? :
construct.
Solution 8:[8]
In Java switch works only with integer literals and those which could be possibly promoted to integer literals such as char.
Moreover in Java boolean type has only two values either true or false. This is unlike the situation in C/C++ where true is any value not equals to zero and false is zero.
Moreover your code is a bit redundant
Solution 9:[9]
You can not use boolean on switch statements. it is no point to use switch when the statement is true or false. it is easy to use if-else.
Solution 10:[10]
Imho switch with boolean would make sense if you could use expressions for case checks instead of if else trees or continues in a loop (maybe I'm wrong that it's a benefit), e.g.:
class Testomatoe
{
private final String country;
private final String sort;
private final int pricePerKiloEUCent;
public Testomtoe
( final String country,
final String sort,
final int pricePerKiloEUCent
)
{
this.country = country;
this.sort = sort;
this.pricePerKiloEUCent = pricePerKiloEUCent;
}
//get...
}
List<Testomataoe> offers = ...
String country;
String sort
int price;
for( Testomatoe tom : offers )
{
country = tom.getCountry();
sort = tom.getSort();
price = tom.getPricePerKiloEUCent();
switch( true )
{
case price < 100:
addToQueryQuality( tom );
break;
case isUrgent( sort ) && price < 300:
adddToPriOrder( tom );
break;
case !country.equals( "Somewheria" ) && price <= 200:
addToOrder( tom );
break;
case country.equals( "Island" ) && sort.equals( "BlueFrost" ) && price < 600:
addToAcceptable( tom );
break;
...
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Tubig |
Solution 3 | |
Solution 4 | Gagan Mani |
Solution 5 | Eric Aya |
Solution 6 | SLaks |
Solution 7 | Gigatron |
Solution 8 | Abhishek Ghosh |
Solution 9 | Mm Mmm |
Solution 10 |