Last Updated on April 18, 2023 by Prepbytes
Menu Driven Programs enhance the interactiveness of the programs. These type of programs takes input from the user and according to the input, shows the output to the user. Here, we will be discussing the Menu Driven Program in Java.
Menu Driven Program
Menu Driven Program in Java is defined as a program that is driven by a menu that presents the user with a list of options from which to choose. These menus are commonly found in devices that use microprocessors, such as washing machines and ATMs.
The use of a menu-driven system offers two benefits. First, input is simplified to a single keystroke, reducing the possibility of errors. Second, the system limits input characters, making them unambiguous. This results in an intuitive and user-friendly interface.
Before discussing the code for the menu driven program in Java, let us first discuss some prerequisites that will assist us in the proper understanding of the menu driven program in Java.
Understanding the Switch-Case in Java
We have certain statements known as conditional statements in programming languages. They are used to execute the code only if a particular condition is satisfied. For instance, let us say that we input a number and we want to know whether this number is even or odd.
The code for the same is shown below.
Even-Odd Program (Understanding If-Else in Java)
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); if(n % 2 == 0) System.out.println("The input number is even"); else System.out.println("The input number is odd"); scn.close(); } }
Input:
5
Output:
The input number is odd
Explanation: The code provided is a basic demonstration of how conditional statements work in Java. If a number can be divided by 2 without leaving a remainder (i.e. number%2=0), it is considered an even number. The "if" keyword is used to verify this condition. On the other hand, if the number cannot be divided by 2, it is an odd number, which is confirmed using the "else" keyword.
We can also use if–else–if–else ladder. For instance, consider the case when we take input from the user and we want to print whether the number is greater than 0, less than 0, or 0 itself. This can be done as shown below.
Understanding If Else-if Else Ladder: Positive, Negative or Zero Program
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); if(n > 0) System.out.println("Greater than 0"); else if(n < 0) System.out.println("Less than 0"); else System.out.println("Equal to 0"); scn.close(); } }
Input:
3
Output:
Greater than 0
So, “if-else” or “if else-if else” is one kind of conditional statement. The other kind of conditional statement in Java is switch-case which is used to create a menu-driven program in Java.
For instance, if any of the following letters “a,e,i,o,u” are entered by the user, we display that they have entered a vowel. Otherwise, we can display that they have entered a consonant. This can easily be done by the if-else statements but now we will do it by using the Switch-Case.
Switch Case Program: Vowels and Consonants
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); char ch = scn.next().charAt(0); switch(ch) { case 'A': System.out.println("It is a vowel 'A'"); break; case 'E': System.out.println("It is a vowel 'E'"); break; case 'I': System.out.println("It is a vowel 'I'"); break; case 'O': System.out.println("It is a vowel 'O'"); break; case 'U': System.out.println("It is a vowel 'U'"); break; default: System.out.println("It is a consonant"); break; } scn.close(); } }
Input:
A
Output:
It is a vowel 'A'
Explanation: In the switch case, we have a switch block, and inside the parenthesis of the switch block we have the variable on which the decision has to be made. Each case is specified separately as shown above and the “break” keyword is used to break out of the switch block if the case has been satisfied. Also, we have a “default” statement. This default statement is used to identify all the cases that are not covered separately above.
So, these switch-cases are used to create what we call as a menu-driven program as these cases act like a menu to choose from.
So, now that we have understood the conditional statements and especially the switch-case that is used to create a menu driven program, let us now learn how we can design an algorithm for Menu Driven Program in Java.
How to design an Algorithm for the Menu Driven Program in Java
Here is how we can design the algorithm for the menu driven program in java by following the given steps.
- Step 1: Start the Java Program
- Step 2: Take the choice of user as an input
- Step 3: Repeat the process till the user is entering a valid choice
- Step 4: In case of a invalid choice, stop the program
Let us now write the code for the Menu Driven Program in Java.
Menu Driven Program in Java – Selecting Engineering Branch
So, the aim of this program is to provide a menu for the students to select their stream in a University. If they choose a stream from the given options, they will be shown the subjects of their chosen stream. Then, they will be asked whether they are sure about the stream they chose or not. If they are sure, they will be displayed the congratulations message on choosing the stream. If they are not sure, they will again be displayed the complete menu. So, the program is as shown below.
Menu Driven Program in Java
import java.util.*; public class Main { public static void cse() { System.out.println("You will get the following subjects"); System.out.println("DSA"); System.out.println("DBMS"); System.out.println("Computer Networks (CN)"); } public static void it() { System.out.println("You will get the following subjects"); System.out.println("DSA"); System.out.println("DBMS"); System.out.println("Computer Networks (CN)"); System.out.println("MPMC (Multi processor and Multi Controller)"); System.out.println("Information Security"); } public static void ece() { System.out.println("You will get the following subjects"); System.out.println("Wireless Communication"); System.out.println("Switching Theory and Logic Design (STLD)"); System.out.println("Mobile Systems Communication"); } public static void ee() { System.out.println("You will get the following subjects"); System.out.println("Switching Theory and Logic Design (STLD)"); System.out.println("Control Systems"); System.out.println("Electrical Circuits and Designs"); } public static void mech() { System.out.println("Force and Friction"); System.out.println("Motors and its Types"); } public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("Welcome to PrepBytes University Portal!!!"); System.out.println("Select one of the following streams in which you want to study"); while(true) { System.out.println("1. Computer Science"); System.out.println("2. Information Technology"); System.out.println("3. Electronics and Communication"); System.out.println("4. Electrical and Electronics"); System.out.println("5. Mechanical"); System.out.println("10. Exit"); System.out.println("Enter the choice number from above"); int choice = scn.nextInt(); switch(choice) { case 1: System.out.println("You have chosen Computer Science."); cse(); System.out.println("Are you sure you want to lock this stream? Choose either A or B."); System.out.println("1. Yes, I'm sure"); System.out.println("2. No, I'm not sure"); int ch = scn.nextInt(); if(ch == 1) { System.out.println("Congratulations!! You are now a CSE student at PrepBytes"); return; } break; case 2: System.out.println("You have chosen Information Technology."); it(); System.out.println("Are you sure you want to lock this stream? Choose either A or B."); System.out.println("1. Yes, I'm sure"); System.out.println("2. No, I'm not sure"); int ch2 = scn.nextInt(); if(ch2 == 1) { System.out.println("Congratulations!! You are now an IT student at PrepBytes"); return; } break; case 3: System.out.println("You have chosen Electronics and Communication."); ece(); System.out.println("Are you sure you want to lock this stream? Choose either A or B."); System.out.println("1. Yes, I'm sure"); System.out.println("2. No, I'm not sure"); int ch3 = scn.nextInt(); if(ch3 == 1) { System.out.println("Congratulations!! You are now an ECE student at PrepBytes"); return; } break; case 4: System.out.println("You have chosen Electrical and Electronics"); ee(); System.out.println("Are you sure you want to lock this stream? Choose either A or B."); System.out.println("1. Yes, I'm sure"); System.out.println("2. No, I'm not sure"); int ch4 = scn.nextInt(); if(ch4 == 1) { System.out.println("Congratulations!! You are now an EEE student at PrepBytes"); return; } break; case 5: System.out.println("You have chosen Mechanical Engineering."); mech(); System.out.println("Are you sure you want to lock this stream? Choose either A or B."); System.out.println("1. Yes, I'm sure"); System.out.println("2. No, I'm not sure"); int ch5 = scn.nextInt(); if(ch5 == 1) { System.out.println("Congratulations!! You are now a ME student at PrepBytes"); return; } break; case 10: break; default: System.out.println("Invalid choice"); break; } if(choice == 10) break; } scn.close(); } }
Output:
Welcome to PrepBytes University Portal!!!
Select one of the following streams in which you want to study
1. Computer Science
2. Information Technology
3. Electronics and Communication
4. Electrical and Electronics
5. Mechanical
10. Exit
Enter the choice number from above
1
You have chosen Computer Science.
You will get the following subjects
DSA
DBMS
Computer Networks (CN)
Are you sure you want to lock this stream? Choose either A or B.
1. Yes, I'm sure
2. No, I'm not sure
1
Congratulations!! You are now a CSE student at PrepBytes
Conclusion
In this article, we have learned about the menu driven program in Java. We have discussed some prerequisites such as if-else, and switch-case statements and then implemented the menu driven program in java. We hope that you have understood the complete concept of a menu driven program in Java.
Frequently Asked Questions(FAQs)
Here are some Frequently Asked Questions related to “Menu Driven Program in Java”.
Ques 1. What are the advantages of using a menu driven program in Java?
Ans. Menu driven Program in Java has following advantages.
- A menu driven program in Java provides a simple and intuitive interface for the user to interact with the program.
- It also reduces the chances of input errors, as the user can select an option from the menu rather than typing in input manually.
- A menu driven program can be easily modified to include new options or functionality.
Ques 2. How do you handle invalid input in a menu driven program in java using switch case?
Ans. To handle invalid input in a menu-driven program using switch case, you can include a default case in the switch statement. This code block can display an error message to the user and prompt them to enter a valid input.
Ques 3. Can a menu driven program in Java using switch case be nested?
Ans. Yes, a menu driven program in Java using switch case can be nested. This means that a switch case statement can be included inside another switch case statement, allowing for multiple levels of options and functionality.
Other Java Programs
Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java