-4

This code is for the purpose of changing months to the corresponding letter codes.

Here is my code I wrote so far:

public static void main (String[ ] args) 

  {
  Scanner input = new Scanner(System.in); 
  System.out.print("Enter month number. [1..12] --> ");
  int month = input.nextInt();
  String MonthString;
  switch (month)
  {
   case 1 : MonthString = "ZS"; break;
   case 2 : MonthString = "CN"; break;
   case 3 : MonthString = "YH"; break;
   case 4 : MonthString = "MT"; break;
   case 5 : MonthString = "CL"; break;
   case 6 : MonthString = "SS"; break;
   case 7 : MonthString = "WM"; break;
   case 8 : MonthString = "WY"; break;
   case 9 : MonthString = "SH"; break;
   case 10 : MonthString = "YJ"; break;
   case 11 : MonthString = "XG"; break;
   case 12 : MonthString = "HZ"; break;
   default : System.out.print("This is not a valid month number.");
  }
  System.out.println(MonthString);/*This is where it won't compile*/
}
Pluto
  • 853
  • 1
  • 8
  • 28
Titus Lin
  • 1
  • 1
  • 2
    Variable names should start in lowercase, so they don't get confused with class names, which start in uppercase. – Progman Oct 17 '20 at 21:26
  • 3
    Your default case doesn't assign `MonthString` to a value. What happens if it is a wrong number? The last statement is an error if MonthString isn't initialized. Either assign it before the switch, assign it in default, or throw an exception. – matt Oct 17 '20 at 21:50

3 Answers3

2

You need to initialize MonthString first:

String MonthString = null;
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • It will compile, but it won't work as intended –  Oct 17 '20 at 21:53
  • @RandomCoder_01 why is that? – Majed Badawi Oct 17 '20 at 21:56
  • He has 2 system.out.println(MonthString). So the output will be "This is not a valid number" followed by "Null". It's not a big deal, but the actual issue is how he was using his default: statement. –  Oct 17 '20 at 21:57
2
default: MonthString = "This is not a valid number";

you need to assign the value in the default of your switch statement, not print it out since it's going to get printed after the switch/case is over.

0

You need to put something on String monthString = ""; first.

Note lowercase and uppercase letters in a variable boot :

When you initialize a variable it should be initialized as follows:

String firstExample= "Hello world"; and not : String FirstExample= "Hello world"; , Classes are given names with capital letters.

public static void main (String[ ] args)

      {
      Scanner input = new Scanner(System.in); 
      System.out.print("Enter month number. [1..12] --> ");
      int month = input.nextInt();
      String monthString = "";
      switch (month)
      {
       case 1 : monthString = "ZS"; break;
       case 2 : monthString = "CN"; break;
       case 3 : monthString = "YH"; break;
       case 4 : monthString = "MT"; break;
       case 5 : monthString = "CL"; break;
       case 6 : monthString = "SS"; break;
       case 7 : monthString = "WM"; break;
       case 8 : monthString = "WY"; break;
       case 9 : monthString = "SH"; break;
       case 10 : monthString = "YJ"; break;
       case 11 : monthString = "XG"; break;
       case 12 : monthString = "HZ"; break;
       default : System.out.print("This is not a valid month number.");
      }
      System.out.println(monthString);
    }
}

Output :

Enter month number. [1..12] --> 1
ZS
Pluto
  • 853
  • 1
  • 8
  • 28