Print Season Name of the Year Based on Month Number in Java



In this article, we will explore different methods to find the season name from the month number in Java. When the user inputs the month number, the output should be the season by the name associated with the month. If the user enters 1 as the month number, the month is January, so the output will be Winter.

Given below is the season classification by months

Winter: December (12), January (1), February (2)

Spring: March (3), April (4), May (5)

Summer: June (6), July (7), August (8)

Autumn: September (9), October (10), November (11)

We can achieve this using several approaches, including if-else statements, switch cases, arrays, and HashMaps.

1.Using If-Else Statements

The if-else method is one of the simplest ways to find the season name based on the month number.

Steps to Implement

Below are the steps to implement this method in Java:

Step 1: Ask the user to enter a month number (1-12).

Step 2: Using if-else statements, check in which season the month belongs.

Step 3: Print the season name.

Step 4: If the number is invalid (below 1 or above 12), display an error message.

Implementation code

The following Java program demonstrates how to determine the season using if-else conditions ?

import java.util.Scanner;
public class tutorialspoint {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter month number (1-12): ");
      int month = sc.nextInt();
      sc.close();
      String season;
      if (month >= 3 && month <= 5) {
         season = "Spring";
      } else if (month >= 6 && month <= 8) {
         season = "Summer";
      } else if (month >= 9 && month <= 11) {
         season = "Autumn";
      } else if (month == 12 || month == 1 || month == 2) {
         season = "Winter";
      } else {
         season = "Invalid month number";
      }
         System.out.println("Season: " + season);
   }
}

Output

Enter month number (1-12): 4
Season: Spring

2.Using Switch case

The switch statement is another way to find the season of the month. It is more readable than a long chain of if-else conditions.

Steps to Implement

Below are the steps to implement this method in Java ?

Step 1: Ask the user for a month number (1-12).

Step 2: Use switch cases to match the month number to its corresponding season. Group the cases for the same seasons.

Step 3: Print the correct season name.

Step 4: If the input does not match any case, display an error message.

Implementation code

The following Java program demonstrates how to determine the season using a switch statement ?

import java.util.Scanner;
public class tutorialspoint {
   public static void main(String[]args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter month number (1-12): ");
      int month = sc.nextInt();
      sc.close();
      String season;
      switch (month) {
         case 12: case 1: case 2:
            season = "Winter";
            break;
         case 3: case 4: case 5:
            season = "Spring";
            break;
         case 6: case 7: case 8:
            season = "Summer";
            break;
         case 9: case 10: case 11:
            season = "Autumn";
            break;
         default:
            season = "Invalid month number";
      }
      System.out.println("Season: " + season);
   }
}

Output

Enter month number (1-12): 6
Beason: Summer

3.Using an Array

In this approach we will use an array to store season names according to the month number as array index.

Steps to Implement

Below are the steps to implement this method in Java ?

Step 1: Create an array and store the season names where index 0-11 corresponds to each month.

Step 2: Ask the user for a month number.

Step 3: Retrieve the season name of the month using the month index.

Step 4: If the input is invalid, print an error message.

Implementation code

The following Java program demonstrates how to determine the season using an array ?

import java.util.Scanner;
public class tutorialspoint {
   public static void main(String[]args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter month number (1-12): ");
      int month = sc.nextInt();
      String[] seasons = {"Winter", "Winter", "Spring", "Spring", "Spring",
         "Summer", "Summer", "Summer", "Autumn", "Autumn", "Autumn", "Winter"};
      if (month >= 1 && month <= 12) {
         System.out.println("Season: " + seasons[month - 1]);
      } else {
         System.out.println("Invalid month number");
      } 
   } 
}

Output

Enter month number (1-12): 9
Beason: Autumn

4.Using hashmap

A HashMap provides a simple and effective method to map month numbers with season names. It allows fast lookups and is especially useful when mappings are non-sequential.

Steps to Implement

Below are the steps to implement this method in Java ?

Step 1: Create a HashMap with month numbers as keys and season names as values.

Step 2: Ask the user to enter a month number.

Step 3: Fetch the season name using the month number as a key.

Step 4: If the input is invalid, show an error message.

Implementation code

The following Java program demonstrates how to determine the season using a HashMap ?

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class tutorialspoint {
   public static void main(String[]args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter month number (1-12): ");
      int month = sc.nextInt();
      sc.close();
      Map<Integer, String> seasonMap = new HashMap<>();
      seasonMap.put(1, "Winter"); seasonMap.put(2, "Winter"); seasonMap.put(12, "Winter");
      seasonMap.put(3, "Spring"); seasonMap.put(4, "Spring"); seasonMap.put(5, "Spring");
      seasonMap.put(6, "Summer"); seasonMap.put(7, "Summer"); seasonMap.put(8, "Summer");
      seasonMap.put(9, "Autumn"); seasonMap.put(10, "Autumn"); seasonMap.put(11, "Autumn");
      System.out.println("Season: " + seasonMap.getOrDefault(month, "Invalid month number"));
   }
}

Output

Enter month number (1-12): 12
Beason: Winter
Updated on: 2025-02-28T14:14:55+05:30

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements