Write a program to read a weekday number and print weekday name using switch statement


This Java program takes an input of a number between 0 and 6 representing a day of the week and outputs the corresponding name of the day. Here's an explanation of the code:

  • import java.util.Scanner;: This line imports the Scanner class from the Java utility package, which allows the program to read user input.
  • public class WeekName: This line begins the class definition for a Java class called WeekName.
  • public static void main(String[] args) : This line begins the main method, which is the entry point of the program.
  • Scanner input = new Scanner(System.in); : This line creates a new instance of the Scanner class called input, which will be used to read user input.
  • int daynum = 0; : This line initializes a variable called daynum to zero, which will later hold the user's input.
  • System.out.printf("Enter the Weekday Number : ");: This line prints a message to the console prompting the user to enter a number representing a day of the week.
  • daynum = input.nextInt();: This line reads the user's input using the Scanner class and stores it in the daynum variable.
  • switch (daynum) : This line begins a switch statement, which evaluates the daynum variable and executes a corresponding block of code based on its value.
  • case 0:: This line starts the first case of the switch statement, which is executed if daynum is 0.
  • System.out.printf("Sunday");: This line prints the string "Sunday" to the console.
  • break;: This line ends the current case and breaks out of the switch statement.
  • The program includes similar code blocks for each of the remaining cases, with each block printing the corresponding day of the week to the console.
  • default:: This line starts the default case, which is executed if daynum does not match any of the other cases.
  • System.out.printf("Invalid...Please Enter the 0 to 6...");: This line prints an error message to the console if the user enters a number outside the range of 0 to 6.
  • break; : This line ends the default case and breaks out of the switch statement.
  • }: This line closes the switch statement.
  • }: This line ends the main method.
  • }: This line ends the WeekName class definition.

Source Code

import java.util.Scanner;
public class WeekName
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int daynum = 0;
 
		System.out.printf("Enter the Weekday Number : ");
		daynum = input.nextInt();
 
		switch (daynum)
		{
		case 0:
			System.out.printf("Sunday");
			break;
		case 1:
			System.out.printf("Monday");
			break;
		case 2:
			System.out.printf("Tuesday");
			break;
		case 3:
			System.out.printf("Wednesday");
			break;
		case 4:
			System.out.printf("Thursday");
			break;
		case 5:
			System.out.printf("Friday");
			break;
		case 6:
			System.out.printf("Saturday");
			break;
		default:
			System.out.printf("Invalid...Please Enter the 0 to 6...");
			break;
		}
	}
}

Output

Enter the Weekday Number : 3
Wednesday