Menu Driven Program in Java

In this article, we will write a menu-driven program in Java. We will learn how to use the switch-case statements to create a menu-driven program in Java. So, let’s get started.

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();
    }
}

Explanation: The code shown above is one of the simplest examples of conditional statements in Java. If a number is divisible by 2 i.e. it leaves the remainder 0 on being divided by 2 i.e. number%2=0, it is an even number. So, we use the “if” keyword to establish this condition. The other case when the number is not divisible by 2 is of the odd number which is established by using the “else” keyword.

We can even have an 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.

Positive Negative Or Zero Program (If Else-if Else Ladder)

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();
    }
}

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 using if-else, however, let’s do it using the switch case as shown below.

Vowels and Consonants (Switch Case Program)

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();
    }
}

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 specially the switch-case that is used to create a menu driven program, let us now create a 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 usre, 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();
    }
}

So, this is a practical example of a menu driven program in Java.

We hope that you have understood the complete concept of a menu driven program in Java. We use the switch case to establish the concept of menu driven program. With this, we come to an end of our discussion on menu driven program in Java. We hope to see you again soon at PrepBytes.

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

Leave a Reply

Your email address will not be published. Required fields are marked *