Java Program with Switch Case

Java Program with Switch Case
Reading Time: 7 minutes

Introduction:

The provided Java program is a simple console-based application that prompts the user to enter a number between 1 and 7. The program then uses a switch case statement to determine the corresponding day of the week based on the user’s input and prints the result.

Algorithm:

  1. Import the Scanner class to read user input.
  2. Create a class named DayOfWeek.
  3. Inside the main method: a. Create a Scanner object to read user input from the console. b. Prompt the user to enter a number between 1 and 7. c. Read the entered number using nextInt() method of the Scanner. d. Declare a String variable named day to store the day of the week. e. Use a switch case statement to match the entered number with corresponding days of the week. f. Print the determined day of the week or “Invalid day number” if the entered number is outside the range 1-7.

Explanation:

  • The program begins by importing the Scanner class, which is used for user input.
  • The DayOfWeek class contains the main method where the execution of the program starts.
  • The user is prompted to input a number between 1 and 7, representing days of the week.
  • The entered number is stored in the variable dayNumber.
  • The program uses a switch case statement to match the dayNumber with the corresponding days of the week.
  • If the entered number matches one of the cases (1-7), the respective day is assigned to the variable day.
  • If the entered number is outside the range, the default case is triggered, assigning “Invalid day number” to the day variable.
  • Finally, the determined day of the week (or the error message) is printed to the console.

This program is a basic example of using a switch case statement in Java to make decisions based on user input.

Code:

    
     import java.util.Scanner;
public class DayOfWeek {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number (1-7): ");
        int dayNumber = scanner.nextInt();
        String day;
        switch (dayNumber) {
            case 1:
                day = "Sunday";
                break;
            case 2:
                day = "Monday";
                break;
            case 3:
                day = "Tuesday";
                break;
            case 4:
                day = "Wednesday";
                break;
            case 5:
                day = "Thursday";
                break;
            case 6:
                day = "Friday";
                break;
            case 7:
                day = "Saturday";
                break;
            default:
                day = "Invalid day number";
        }
        System.out.println("Day of the week: " + day);
    }
}

    
   

Input :

    
     Enter a number (1-7): 7
    
   

Output :

    
     Day of the week: Saturday
    
   

This program prompts the user to enter a number between 1 and 7, inclusive, representing a day of the week. It then uses a switch case to determine the corresponding day and prints it. If the user enters a number outside this range, it prints “Invalid day number.”

FAQs :

Question: How can I create a Java program that utilizes a switch case statement?

Answer: To implement a Java program with a switch case, you can use the switch statement to handle multiple cases based on a given input, making your code more readable and efficient.

Question: What is the significance of a switch case in a Java program?

Answer: In a Java program, a switch case provides a structured way to handle multiple possible values of a variable, enhancing code clarity and maintainability compared to using multiple if-else statements.

Question: Are there any specific scenarios where using a switch case in a Java program is recommended?

Answer: Yes, a switch case is particularly useful when you have a variable with discrete values and you want to execute different code blocks based on each possible value, offering a concise and organized solution.

Question: Can you share an example of a Java program using a switch case to solve a common problem?

Answer: Certainly! Consider a program that takes a numeric input representing a day of the week and uses a switch case to determine and display the corresponding day, showcasing the switch statement’s practical application.

Question: How does using a switch case contribute to the efficiency of a Java program compared to alternative decision-making structures?

Answer: Switch cases in Java are optimized for scenarios with multiple discrete values, providing a more efficient and readable solution than a chain of if-else statements. This makes the code easier to understand and maintain, especially when dealing with a large number of cases.