Connect with us

java

How to create menu driven program in java

Published

on

menu driven program in java


Menu driven program in java :-
Hi Folks, Thanks for coming back. Today I will show to how to make a menu driven program in java.

First of all, let me introduce a little bit regarding java language. Java is a really popular and effective programming language. Its first beta version release on market in 1995 after that its first version(Oak) came in the market in January 1996.

Why java is Popular ?

Java allows the user to write once and play anywhere because Java runs on its JVM (java virtual machine). Java is a really user-friendly, powerful and safe programming language.

Menu driven program in java

Why we need menu driven program?

Menu driven program basically provide a choice to application’s user thereby complexity of the code reduced a lot and helps the user to go with the flow. Also, menu driven program helps to combine multiple program into the single program.

Suppose you have multiple programs and you want to combine them into a single program then you will need to use menu driven technique.

Read Also:-
1. 4 Best IDE for javascript development
2. HTML interview questions and answers for freshers

import java.util.Scanner;

public class Menu {
	private static Scanner scanner;
	
	public static void showMenu() {
		System.out.println("01. Prime number");
		System.out.println("02. Palindrome number");
		System.out.println("03. Exit");
		System.out.print("Enter Your Choice : ");
	}
	
	public static void calculatePrime(int number) {
		int minNumber = 2;
		boolean isPrime = true;
		while (minNumber <= number / 2) {
			if (number % minNumber == 0) {
				isPrime = false;
				break;
			}
			minNumber++;
		}
		if (isPrime) {
			System.out.println(number + " is a prime Number");
		} else {
			System.out.println(number + " is not a prime Number");
		}
	}
	
	public static void calculatePalindrom(int number) {
		int rev = 0;
		int temp = number;
		while (temp > 0) {
			rev = (rev * 10) + (temp % 10);
			temp /= 10;
		}
		if (number == rev) {
			System.out.println(number + " is a palindrom No.");
		} else {
			System.out.println(number + " is not a palindrom No.");
		}
	}
	
	public static void main(String[] args) {
		scanner = new Scanner(System.in);
		int choice;
		int number;
		while (true) {
			showMenu();
			choice = scanner.nextInt();
			switch (choice) {
			case 1:
				System.out.println("Enter A Number:");
				number = scanner.nextInt();
				calculatePrime(number);
				break;
			case 2:
				System.out.println("Enter A Number:");
				number = scanner.nextInt();
				calculatePalindrom(number);
				break;
			case 3:
				scanner.close();
				System.exit(0);
			default:
				System.out.println("Wrong Entry");
			}
		}
	}
}

Code explanation

Code explanation of menu driven program in java.
Alright, let’s elaborate with some simplified explanation. In the above code,
Code flow starts with public static void main string args.

We are using a Scanner class which resides into the java.util package. Scanner class basically used to obtain the input from the user. We are using a method nextInt() it is to used to grab the value from the user via console/command prompt.

Also, we are using Scanner System.in our program and passing it to the Scanner’s constructor.

After that, We are using a while loop which is responsible to continue the program execution until the user exit.

scanner.nextInt() Method is obtaining the input from the console and we are passing that int value into the switch choice case.

As per the user’s input switch case decide which code should be executed.
If the user passes 1 then calculatePrime() method will call.
If the user passes 2 then calculatePalindrom () method will call.
And if the user passes 3 then program execution will stop. System.exit(0) is stopping the code execution.

When a break case statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement

If you still have any doubt on menu driven program in java please comment below.

Continue Reading
Click to comment

Leave a Reply

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

Trending