Question

I'm currently learning java from a book, and a project was to output the days and month name of a month after taking in the number of the month. I was wondering if there is any better way to set up my if statement rather then what I have already done.

PS:console reader is just an included class to easily take input from the users console.

public class Project13 {

public static void main(String[] args) {
    ConsoleReader console = new ConsoleReader(System.in);

    System.out.println("Enter a month you would like to evaluate (by number):");
    int month = console.readInt();

    int days = 0;
    String monthout = "Month";
    String out = "Yes";
    if(month == 1){
        days = 31;
        monthout = "January";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 2){
        System.out.println("Is it a leap year? Yes or No:");
        String leap = console.readLine(); 
        if(leap.equalsIgnoreCase("yes")){
            days = 29;
            monthout = "February";
            out = "There are " + days + " days in " + monthout;
        }else if(leap.equalsIgnoreCase("no")){
            days = 28;
            monthout = "February";
            out = "There are " + days + " days in " + monthout;
        }else{
            out = "Something went wrong, please try again";
        }
    }else if(month == 3){
        days = 31;
        monthout = "March";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 4){
        days = 30;
        monthout= "April";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 5){
        days = 31;
        monthout = "May";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 6){
        days = 30;
        monthout = "June";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 7){
        days = 31;
        monthout = "July";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 8){
        days = 31;
        monthout = "August";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 9){
        days = 30;
        monthout = "September";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 10){
        days = 31;
        monthout = "October";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 11){
        days = 30;
        monthout = "November";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 12){
        days = 31;
        monthout = "December";
        out = "There are " + days + " days in " + monthout;
    }else if(month > 12){
        out = "Your month input was not valid. Please try again.";
    }

    System.out.println(out);
}

}
Was it helpful?

Solution

You can replace almost the entire if statement with a pair of arrays like this:

int dayCount[] = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String monthName[] = new String[] {"January", "February", ...};

With these two arrays in hand, you can do this:

// February is the only month that needs special handling
if (month == 2) {
    // Do your special handling of leap year etc...
} else if (month >= 1 && month <= 12) {
    // All other valid months go here. Since Java arrays are zero-based,
    // we subtract 1 from the month number
    days = dayCount[month-1];
    monthout = monthName[month-1];
} else {
    // Put handling of invalid month here
}
out = "There are " + days + " days in " + monthout;

OTHER TIPS

switch(month) {
    case 1:
        // stuff
        break;
    case 2:
        // etc.....

And if you're going to do this, you should definitely use an enum rather than just integers representing months...

Swtich:

switch (month)
{
    case 1:
        // ... stuff here
        break;
    case 2:
        // ... stuff here
        break;
    // ... more cases
    default: // no case above matched what the month was
        // .... handle default case
};

Use switch statement instead of many else-if. It's easier to read, modify and with breaks works faster then else-if

case 1:
//code
break; 

You can go with switch-case statements.Avoid so many if-else

switch(month) {
    case 1:
        days = 31;
        monthout = "January";
        out = "There are " + days + " days in " + monthout;
        break;
    case 2:
        // Add stuff
         break;
    default:
        break;
}

I think you should use a Map to avoid all of this if (or switch) cases. The main problem of your code is that your a duplicating lot of code.

private static Map<Integer, Integer> monthDays = new HashMap<>();

static {
    monthDays.put(1, 31);
    monthDays.put(2, 28);
    ...
}

private static int getMonthDays(int month) {
    if (month == 2) {
        // handle special case with Februar
    }
    return monthDays.get(month);
}

public static void main(String[] args) {
   ...    
   if (month >= 1 && month <= 12) {
       monthout = getMonthDays(month);
       ...
       out = "There are " + days + " days in " + monthout;
   } else {
       out = "Your month input was not valid. Please try again.";
   }
   System.out.println(out);
}

switch-case is no better. If there is more than 3-4 if-else or switch condition think about some other way doing this like command or strategy pattern or something.

For example create a map of actions which set and do things you want in if condition. Sample code is below. This is just sample code let me know in case you need any further elaboration for any part:

public void initMap(){
         Map<Integer, MonthData> monthDataMap = new HashMap<Integer,MonthData>();
         monthDataMap.put(1,new JanData());
         monthDataMap.put(2,new FebData());

     }

    interface MonthData {
        public String getMonth();
        public int getDays();
        public String getOut();
    }

    class JanData  implements MonthData{
        private String month ="January";
        private int days = 30;
        private String out =  "There are " + days + " days in " + month;

        @Override
        public String getMonth() {
            return month;
        }

        @Override
        public int getDays() {
            return days;
        }

        @Override
        public String getOut() {
            return out;
        }
    }


    class FebData {
        private String month ="February";
        private int days = 28;
        private String out =  "There are " + days + " days in " + month;
    }

    ....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top