0
incompatible types java
I have written this program to identify if the days of the weeks are even or odd. https://code.sololearn.com/cQ48Y9PIgdE6 I have the error of incompatible types and I couldnt fix this. any idea?
2 Respuestas
+ 1
Yes. The day is of string type but your cases are Boolean types..
It must be like :
sample:
import java.util.*;
public class Program
{
public static void main(String[] args) {
String day;
Scanner sc= new Scanner(System.in);
day = sc.nextLine();
switch (day) {
case "Saturday" :
System.out.println("Even days");
break;
case "Sunday":
System.out.println("Odd days");
break;
default:
System.out.println("Friday");
}
}
}
edit:
Sagheb
you can simply use multi values by camma separated for string like
case "Monday","Wednesday","Saturday" :
+ 1
Sagheb
You are using Boolean value in cases which doesn't accept because Boolean value may have only true or false but in switch there maybe many cases so you cannot write true or false twice in case like
case true:
//statement
break;
case true:
//statement
break;
This will give error because of duplicate case that's why we can't write boolean values in switch case.