In Java, there are multiple ways to display the weekdays in a week, we can use if-else-if conditions, the ternary operator, or switch statements. In this article we will be learning through examples, detailed logic, and program explanation for understanding.
For example, if the input is number 1, then the resulting day is “Monday”.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to write a Java program that displays the weekday corresponding to a given input week. The program should take input values from 1 to 7, and the output should be the corresponding weekday. This above action should be done in our program. Let’s look at an example,
Example 1:
Input: 1
Output: Monday
Here, for the input 1, Output is “Monday”.
Example 2:
Input: 7
Output: Sunday
Here, for the input 7, output is “Sunday”.
Logic to display weekdays in a week in Java
There are multiple logics to display weekdays in a Week in the Java Programing language.
Logic 1: Using if-else-if
- After receiving the week number input from the user, the program compares this number with values 1 through 7, representing the days of the week. If the number equals 1, it prints ‘Monday’, if it equals 2, it prints ‘Tuesday’, and so forth using if-else-if. This process continues for each day of the week, ensuring the correct weekday is displayed based on the user’s input.
int day = s.nextInt();
if(day==1)
System.out.println("Monday");
else if(day==2)
System.out.println("Tuesday");
else if(day==3)
System.out.println("Wednesday");
else if(day==4)
System.out.println("Thursady");
else if(day==5)
System.out.println("Friday");
else if(day==6)
System.out.println("Saturday");
else if(day==7)
System.out.println("Sunday");
else
System.out.println("Please choose between 1 to 7");
Logic 2: Ternary Operator
- The ternary operator takes three operands and functions similarly to if-else statements. If the condition is true, it evaluates the first expression, if false, it evaluates the second expression.
- In this program, it takes an input week number from the user and uses a string variable to store the result. For example, if the number equals 1, it stores ‘Monday’ in the result variable and prints ‘Monday’. This comparison and assignment process repeats similarly for all days of the week
int day = s.nextInt();
String result;
result =(day==1) ? day+" Monday":
(day==2)? day +" Tuesday":
(day==3)? day +" Wednesday":
(day==4)? day +" Thursady":
(day==5)? day+ " Friday":
(day==6)? day+ " Saturday":
(day==7)?day+ " Sunday":
day+"please choose between 1 to 7";
System.out.println(result);
Logic 3: Using Switch Statements
- The switch statement in Java is a value-based conditional statement where each case represents a specific value. Here, we indirectly check for equality conditions.
- For example, if the case is 1, it prints ‘Monday’. If not, it proceeds to the next case and continues this process until case 7. The default case is used to inform the user to select a number between 1 and 7 if none of the specified cases match.
- To ensure only the requested day is printed, we use a break statement after each case. Without break, the switch statement would execute all subsequent cases after finding a match, which is not desired
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("please choose between 1 to 7");
Java Program to display weekdays in a week
Program 1: Using if-else-if
In the below program we are using if-else-if to display weekdays in a week in java.
import java.util.Scanner;
public class Weekdays {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Input number between 1-7:");
int day = s.nextInt();
if(day==1)
System.out.println("Monday");
else if(day==2)
System.out.println("Tuesday");
else if(day==3)
System.out.println("Wednesday");
else if(day==4)
System.out.println("Thursady");
else if(day==5)
System.out.println("Friday");
else if(day==6)
System.out.println("Saturday");
else if(day==7)
System.out.println("Sunday");
else
System.out.println("please choose between 1 to 7");
s.close();
}
}
Output:
Input number between 1-7:
5
Friday
Java program explanation to print week day for given input week
- The program imports Scanner class from java.util package.
- Program defines a class named
Weekdays
. - The program contains the main method where the program execution starts.
- Asks the user to choose a number between 1 and 7 to represent a day of the week.
- Reads the user input number using the
Scanner
object’snextInt()
method. - It uses a series of
if
andelse if
statements to determine the day of the week corresponding to the input number.- If the input number is 1, it prints “Monday”.
- When the input number is 2, it prints “Tuesday”.
- If the input number is 3, it prints “Wednesday”.
- When the input number is 4, it prints “Thursday”.
- If the input number is 5, it prints “Friday”.
- When the input number is 6, it prints “Saturday”.
- If the input number is 7, it prints “Sunday”.
- When the input number is outside the range of 1 to 7, it prints a message prompting the user to choose a number between 1 and 7
- It closes the
Scanner
objects
to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to display weekdays in a week in Java.
import java.util.Scanner;
public class Weekdays {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Input number between 1-7:");
int day = s.nextInt();
String result;
result =(day==1) ? day+" Monday":
(day==2)? day +" Tuesday":
(day==3)? day +" Wednesday":
(day==4)? day +" Thursady":
(day==5)? day+ " Friday":
(day==6)? day+ " Saturday":
(day==7)? day+ " Sunday":
day+"please choose between 1 to 7";
System.out.println(result);
s.close();
}
}
Output:
Input number between 1-7:
7
7 Sunday
Java program explanation to print week day for given input week
- The program imports the
Scanner
class from thejava.util
package to facilitate user input. - Program defines a class named
Weekdays
. - The program contains the main method where the program execution starts.
- Asks the user to choose a number between 1 and 7 to represent a day of the week.
- Reads the user input number using the
Scanner
object’snextInt()
method. - It uses a series of ternary operators to determine the day of the week corresponding to the input number and assigns the result to the
result
variable.- If the input number is 1, it assigns “Monday” to the
result
variable. - When the input number is 2, it assigns “Tuesday” to the
result
variable. - If the input number is 3, it assigns “Wednesday” to the
result
variable. - If the input number is 4, it assigns “Thursday” to the
result
variable. - When the input number is 5, it assigns “Friday” to the
result
variable. - If the input number is 6, it assigns “Saturday” to the
result
variable. - When the input number is 7, it assigns “Sunday” to the
result
variable. - If the input number is outside the range of 1 to 7, it assigns a message prompting the user to choose a number between 1 and 7 to the
result
variable. - Program prints the value of the
result
variable. - It closes the
Scanner
objects
to release system resources.
- If the input number is 1, it assigns “Monday” to the
Program 3: Using Switch Statement
In the below program we are using switch statement to display weekdays in a week in Java.
import java.util.Scanner;
public class Weekdays {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Input number between 1-7:");
int day = s.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Please choose between 1 to 7");
}
s.close();
}
}
Output:
Input number between 1-7:
7
Sunday
Java program explanation to print week day for given input week
- The program starts by importing the
Scanner
class from thejava.util
package. - It defines a class named
WeekdaysSwitchStatements
with amain
method, which serves as the entry point of the program. - Inside the
main
method:- It creates a new instance of
Scanner
nameds
to read input from the console. - Asks the user to choose a number from 1 to 7 to represent a day of the week.
- It reads the user’s input using the
nextInt()
method of theScanner
class and stores it in the variableday
.
- It creates a new instance of
- The program uses a
switch
statement to determine the day corresponding to the user’s input:- If the value of
day
matches any of the case values (1 to 6), the corresponding day of the week is printed. - When the value of
day
doesn’t match any of the case values, thedefault
case is executed, and “Sunday” is printed.
- If the value of
- After printing the corresponding day, the program closes the
Scanner
objects
to release any associated resources.
Conclusion
In this article, we explored how to display weekdays in a week using if-else-if statements, ternary operator and switch statement. We understood the logic behind this and moved on to writing the program.
For more information on related topics, you might find these articles helpful:
- Understanding the concepts of Conditional statements in Java
- Concepts of operators in Java
- Top 10 Programming tips in Java
We hope you find this tutorial helpful. If you have any questions or need any further clarification, feel free to leave a comment below. Don’t forget to subscribe our newsletter to get more updates related to Java tutorials and tips.
Happy Java learning!