public String getCurrentDay()
   {
       if(day.getValue() == 0) {
          return "Sunday";
       }   
       if(day.getValue() == 1) {
          return "Monday";    
       }
       if(day.getValue() == 2) {
          return "Tuesday";
       }    
       if(day.getValue() == 3) {
          return "Wednesday";
       }   
       if(day.getValue() == 4) {
          return "Thursday";
       }
       if(day.getValue() == 5) {
          return "Friday";
       }
       if(day.getValue() == 6) {
          return "Saturday";
       }    
       return "null";
   }

How do I make this into a switch statement?

Dani AI

Generated

A short diagnosis and a couple of safe fixes.

You saw the "incompatible type" error because the switch selector must be one of the supported types (integral primitives/wrappers, enum types, or String) — it won't accept arbitrary objects (for example a UI control or some custom class). Confirm the declared type of day (or whether day.getValue() returns an int) and pick the right selector accordingly. (docs.oracle.com)

If day.getValue() is the integer you expect, switch on that value and return from each case (that avoids needing break). Example:

public String getCurrentDay() {
    int d = day.getValue(); // verify this is an int
    switch (d) {
        case 0: return "Sunday";
        case 1: return "Monday";
        case 2: return "Tuesday";
        case 3: return "Wednesday";
        case 4: return "Thursday";
        case 5: return "Friday";
        case 6: return "Saturday";
        default: throw new IllegalArgumentException("Invalid day: " + d);
    }
}

If day is an enum (preferred for readability), switch on the enum constants instead. Using java.time.DayOfWeek (recommended for date logic) the enum values are MONDAY..SUNDAY; note its numeric mapping is 1 (Monday) to 7 (Sunday) if you need to convert from a 0-based index. Example conversion:

int idx = day.getValue();                // 0 == Sunday, 1 == Monday, ...
DayOfWeek dow = DayOfWeek.of(((idx + 6) % 7) + 1); // maps 0..6 -> 7..6->1..7

and then switch on dow. See the DayOfWeek API for the value mapping. (docs.oracle.com)

Practical troubleshooting checklist based on the thread: (1) check the declared type of day, (2) correct the typo seen in your post (daysString vs dayString), (3) ensure every code path returns a value (your method must return a String), and (4) prefer enums or java.time types over magic numbers. was right that break prevents fall‑through; using return inside cases is often clearer for simple mappings. and were also pointing in the right direction about using the right selector type and Java version differences.

Recommended Answers

All 6 Replies

you don't have to read a lot to understand a switch statement. It works like this:

switch(variable)
{
case 1:
    //do something
    break;
case 2:
    //do something
    break;
case 3:
    //do something
    break;
//...
default://in case it's an unknown case
    //do something
    break;
}

In your case, break;'s are not necessary. Break is used to go to the first line after the switch.

public String getCurrentDay()
    {
   switch(day) {

        case 0: dayString = "Sunday";
                break; 
        case 1: dayString = "Monday"; 
                break;
        case 2: daysString = "Tuesday";
                break;
        case 3: dayString = "Wednesday";
                break;
        case 4: dayString = "Thursday";
                break;
        case 5: dayString = "Friday";
                break; 
        case 6: dayString = "Saturday";        
    }
   }  

I tried to compile this and an error message said that the varible (day) is an incompatible type, what do I do?

I am taking an introductory software development course. Please understand...

ok the variable is ("day")

@ SuperManofBC,
day should be integer variable,
check this link

Hey thanks that helped..

Anand01: it's maybe a bit late for this response, but anyway: in this example it should indeed be an int, but starting from java 7 it is possible to use a String variable as value in a Switch, there's an example in the link you've posted.
so it just depends on what JDK is installed and what the goal is, whether it should be an int or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.