Home > Blockchain >  Java program using switch case seasons of the year getting error
Java program using switch case seasons of the year getting error

Time:12-30

I am trying out a program below are the details

Write a program to take a month number and print if it is summer, winter,rainy season Month number - 1to 12 Let's assume Winter: Nov,Dec,Feb Summer: Mar,April,May,June, July Rainy: July,August,Sep,Oct

Note: you can use default to handle rainy, and just 2 cases to handle winter and summer

# Solution 1 - only testing the logic

package java_cls;

public class switch {
    public static void main (String[] args) {
        int month = 6;
        switch (month) {
            case 1:
                System.out.println("January");
                System.out.println("Winter");
                break;
            case 2:
                System.out.println("February");
                System.out.println("Winter");
                break;
            case 3:
                System.out.println("March");
                System.out.println("Summer");
                break;
            case 4:
                System.out.println("April");
                System.out.println("Summer");
                break;
            case 5:
                System.out.println("May");
                System.out.println("Summer");
                break;
            case 6:
                System.out.println("June");
                System.out.println("Summer");
                break;
            case 7:
                System.out.println("July");
                System.out.println("Summer");
                break;
            case 8:
                System.out.println("August");
                System.out.println("Rainy");
                break;
            case 9:
                System.out.println("September");
                System.out.println("Rainy");
                break;
            case 10:
                System.out.println("October");
                System.out.println("Rainy");
                break;
            case 11:
                System.out.println("November");
                System.out.println("Winter");
                break;
            case 12:
                System.out.println("December");
                System.out.println("Winter");
                break;

        }

    }
}

# solution 2 - Actual solution

public class ifelse {
    public static void main (String[] args) {

        String summer = "March,April,May,June,July";
        String winter = "November,December,January,February";
        String rainy = "August,September,October";

        int month = 1;

        switch (summer) {
            case 1:
                System.out.printf("Summer");
                break;
        }
        switch (winter) {
            case 2:
                System.out.printf("Winter");
                break;
        }
        switch (rainy) {
            case 3:
            default:

        }
    }
}

from solution 2 I was not able to construct the final solution , any leads will be helpful

CodePudding user response:

Write a program to take a month number and print if it is summer, winter,rainy season Month number - 1to 12 Let's assume Winter: Nov,Dec,Feb Summer: Mar,April,May,June, July Rainy: July,August,Sep,Oct

Note: you can use default to handle rainy, and just 2 cases to handle winter and summer

There's no way to make it done using switch-statements with literally only 2 case labels, but Java allows to group case labels and associate the group of labels with a single statement.

So as @Old Dog Programmer has pointed out in the comments, you can create 2 groups of case labels (for "Winter" and for "Summer"), and caver the "Rainy" season using default label.

In order to do that you need to define an array containing the names of months, and then inside the switch refer its elements.

That how it might look like:

int month = // initalizing the month variable
        
String[] months = {
    "January", "February",                   // Winter
    "March", "April", "May", "June", "July", // Summer
    "August", "September", "October",        // Rainy
    "November", "December"                   // Winter
};
        
int monthIndex = month - 1;
        
switch (monthIndex) {
    case 0: case 1: case 10: case 11:
        System.out.println(months[monthIndex]);
        System.out.println("Winter");
        break;
    case 2: case 3: case 4: case 5: case 6:
        System.out.println(months[monthIndex]);
        System.out.println("Summer");
        break;
    default:
        System.out.println(months[monthIndex]);
        System.out.println("Rainy");
}

I guess that's what you're required to do.

Just for informational purposes, here's a couple of more advanced options.

Java 14 switch-expressions

switch-statement shown above can be written in a more concise way using switch-expressions (no need to repeat case and use break statements):

switch (monthIndex) {
    case 0, 1, 10, 11 -> {
        System.out.println(months[monthIndex]);
        System.out.println("Winter");
    }
    case 2, 3, 4, 5, 6 -> {
        System.out.println(months[monthIndex]);
        System.out.println("Summer");
    }
    default -> {
        System.out.println(months[monthIndex]);
        System.out.println("Rainy");
    }
}

Map

Another option is to associate a Month with a corresponding season is to use a Map, this approach doesn't require resorting to conditional statements.

In order to make the code for generating a Map I've use Stream API and Collector toMap().

The overall logic remains the same: map contains information about the "Winter" and "Summer", the case with the "Rainy" season is covered using Map.getOrDefault() method.

Here's how it might look like:

public static final String[] MONTHS = {
    "January", "February",                   // Winter
    "March", "April", "May", "June", "July", // Summer
    "August", "September", "October",        // Rainy
    "November", "December"                   // Winter
};

public static final Map<Integer, String> SEASON_BY_MONTH =
    Stream.concat(
            IntStream.of(0, 1, 10, 11).mapToObj(i -> Map.entry(i, "Winter")),
            IntStream.of(2, 3, 4, 5, 6).mapToObj(i -> Map.entry(i, "Summer"))
        )
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

public static void displayMonth(int month) {
    int monthIndex = month - 1;
    
    System.out.println(MONTHS[monthIndex]);
    System.out.println(SEASON_BY_MONTH.getOrDefault(monthIndex, "Rainy"));
}

CodePudding user response:

since the resolution of your problem doesn't seem to need the name of the month you can do like this if you really want just 2 cases and a default on the switch statement

public class Main {
    public static void main(String[] args) {
            int monthNum = 1;
            Season season = Season.getSeason(monthNum);
            if(season == null) return;
            switch (season) {
                case SUMMER:
                    System.out.println("Summer");
                    break;
                case WINTER:
                    System.out.println("Winter");
                    break;
                default:
                    System.out.println("Rainy");
                    break;
            }
        }
}

--

enum Season {
        WINTER(List.of(1, 2, 11, 12)),
        SUMMER(List.of(3, 4, 5, 6, 7)),
        RAINY(List.of(8, 9, 10));

        private final List<Integer> months;

        Season(List<Integer> months) {
            this.months = months;
        }

        public static Season getSeason(int num) {
            for (Season season : Season.values()) {
                if (season.months.contains(num)) return season;
            }
            return null;
        }
    }

-- This one is only if you want to also print the months name

public class Main {
    public static void main(String[] args) {
        String[] months = new String[]{
                "January", "February", "March", "April",
                "May", "June", "July", "August",
                "September", "October", "November", "December"};
        int monthNum = 12;
        Season season = Season.getSeason(monthNum);
        if (season == null) return;
        switch (season) {
            case SUMMER:
                System.out.println("Summer");
                System.out.println(months[monthNum - 1]);
                break;
            case WINTER:
                System.out.println("Winter");
                System.out.println(months[monthNum - 1]);
                break;
            default:
                System.out.println("Rainy");
                System.out.println(months[monthNum - 1]);
                break;
        }
    }
}
  • Related