java - Switch Statements with months -


my assignment print formatted string month # nnnn (where # sign value of field monthnumber , nnnn value of field monthname. problem is, when click getmonthname, comes out 'null'.

how fix that?

public class month {     // instance variables - replace example below own     private int monthnumber;     private string monthname;     private string newmonthname;      /**      * constructor objects of class month      */     public month(int input){          if((input == 0) || (input > 12)){             system.out.println("error: month number must between 1 , 12");         }         else if(input < 0){             system.out.println("error: month number must positive");         }         else{ //the value of input valid             system.out.println("month number valid");         }         setmonthname(monthname);         printmonth(input);         monthnumber = input;     }      /**      *       */     private void setmonthname(string monthname)     {         switch(monthnumber){             case 1:                 monthname = "january";                 break;             case 2:                 monthname = "february";                 break;             case 3:                 monthname = "march";                 break;             case 4:                 monthname = "april";                 break;             case 5:                 monthname = "may";                 break;             case 6:                 monthname = "june";                 break;             case 7:                 monthname = "july";                 break;             case 8:                 monthname = "august";                 break;             case 9:                 monthname = "september";                 break;             case 10:                 monthname = "october";                 break;             case 11:                 monthname = "november";                 break;             case 12:                 monthname = "december";                 break;             default:                 monthname = "invalid month";                 break;         }      }      /**      *       */     private void printmonth(int input)     {         switch(input){             case 0:                 system.out.println("month " + input + " not month");                 break;             case 1:                 system.out.println("month " + input + " january");                 break;             case 2:                 system.out.println("month " + input + " february");                 break;             case 3:                 system.out.println("month " + input + " march");                 break;             case 4:                 system.out.println("month " + input + " april");                 break;             case 5:                 system.out.println("month " + input + " may");                 break;             case 6:                 system.out.println("month " + input + " june");                 break;             case 7:                 system.out.println("month " + input + " july");                 break;             case 8:                 system.out.println("month " + input + " august");                 break;             case 9:                 system.out.println("month " + input + " september");                 break;             case 10:                 system.out.println("month " + input + " october");                 break;             case 11:                 system.out.println("month " + input + " november");                 break;             case 12:                 system.out.println("month " + input + " december");                 break;             default:                 system.out.println("month " + input + "is invalid month");                 break;         }     }             /**      *       */     public int getmonthnumber()     {         return monthnumber;     }      /**      *       */     public string getmonthname()     {         return monthname;     }  } 

sorry seem assign monthnumber after calling setmonthname().

rearrange code in constructor:

   monthnumber = input;    setmonthname();    printmonth(input); 

also, in setmonthname(), assign parameter, due having same name overshadows field in class. (it idea turn on warning in ide) remove parameter setmonthname(), set field in object.

private void setmonthname() {     switch(monthnumber){     [...]     } } 

finally, unrelated problem asked, recommend changing printmonth(int input) use fields in object instead of duplicating switch statement setmonthname().


Comments