Trying to understand why nested switch case statement is not compiling

Viewed 89

So I'm trying to figure out why my IDE is not accepting this piece of code. The error it's giving me is that the return statement is missing. But after doing some digging I found out that when I already have a return statement I don't really need a break. Heres me code.

The month, day, and year are private variables that have already been initialized in the private constructor. Each of the switch cases is supposed to take the date inputs and out the date in a specific format.

public String toString(int format) {
  switch (format) {
    case 1:
      return month + "/" + day + "/" + year;
    case 2:
      String pattern = "00";
      DecimalFormat decimalFormat = new DecimalFormat(pattern);
      return decimalFormat.format(month) + "/" +
        decimalFormat.format(day) + "/" +
        year;
    case 3:
      String abbrev = "JanFebMarAprMayJunJulAugSepOctNovDec";
      return abbrev.substring(month * 3 - 3, month * 3 - 1) +
        "/" + day + "/" + year;
    case 4:
      switch (month) {
        case 1:
          return "January" + " " + day + ", " + year;
        case 2:
          return "February" + " " + day + ", " + year;
        case 3:
          return "March" + " " + day + ", " + year;
        case 4:
          return "April" + " " + day + ", " + year;
        case 5:
          return "May" + " " + day + ", " + year;
        case 6:
          return "June" + " " + day + ", " + year;
        case 7:
          return "July" + " " + day + ", " + year;
        case 8:
          return "August" + " " + day + ", " + year;
        case 9:
          return "September" + " " + day + ", " + year;
        case 10:
          return "October" + " " + day + ", " + year;
        case 11:
          return "November" + " " + day + ", " + year;
        case 12:
          return "December" + " " + day + ", " + year;
        default:
          System.out.println("Error please try again");
          break;
      }
      default:
        System.out.println("Error please try again");
  }
}
1 Answers

Your method is supposed to return a String.

This means: any path that leads to the "exit" of your method, needs to return a value.

default:
  System.out.println("Error please try again");

Does not return a value. It prints a message.

In other words: your two default cases should either return a string, such as "invalid input". But in the real world, this isn't what you want.

Your code seems to be about some sort of Date class. And your toString(int) implementation allows for invalid data. That is where your problem starts! You should validate your fields at some point. But that point is not when you are asked to format it. If you do it at that point, that method should rather throw an exception to indicate: "can't do my work because of bad data".

So, two choices: either your code validates all user inputs when the user provides them (and refuses bad data), or your toString() method should throw an exception on invalid data.

Related