A Java Console Menu Application is a great way to start learning programming. With a menu console application, you can easily test your code and see results after giving certain inputs.
In this post, I’ll show how to create a Java console menu application.
To create a menu for a Java console application, I recommend you the following:
- show the menu options within an infinite loop
- get input from the user on what option he/she wants to execute
- use conditional statements to perform an action depending on the user input.
Table of Contents
- Printing the menu in the Java console app
- Add exception handling
- Implementing actions for each option of the console menu
- Example of Java console menu for simple calculations
- Summary
Printing the menu in the Java console app
The first step for us to create a menu is to print the options on the screen.
To do that, we need to know what are the options that are available on our menu.
One can do this in several ways. In this case, I will use a string array to store all the options. Notice that each string has a respective number at the beginning.
We will use this number later to know what action to execute.
Find the code below.
public class Main { public static void printMenu(String[] options){ for (String option : options){ System.out.println(option); } System.out.print("Choose your option : "); } public static void main(String[] args) { String[] options = {"1- Option 1", "2- Option 2", "3- Option 3", "4- Exit", }; Scanner scanner = new Scanner(System.in); int option; while (true){ printMenu(options); option = scanner.nextInt(); } } }
From the previous code, you can see the following three main things:
- the array of options, to store the options we want to print on the screen.
- a method that prints all the options.
- an infinity loop that prints the menu at the beginning of the console app execution, and after the user choose an option and the respective action is carried on.
This design model the way the menu console application works. A user enters an option using the keyboard, an action is executed according to the option and the user is asked for another option. If the option is to exit, then the computer finishes the execution of the console application.
Add exception handling
A simple Java console menu is not the “exception” when it comes to creating robust software.
By robust software we mean an application that can recover from errors and that won’t crash.
To create a robust java console menu we should use exception handling. If you want to read more about exception handling in Java, you can read this post. The post offers a wide view of exception handling and has several examples.
public class Main { public static void printMenu(String[] options){ for (String option : options){ System.out.println(option); } System.out.print("Choose your option : "); } public static void main(String[] args) { String[] options = {"1- Option 1", "2- Option 2", "3- Option 3", "4- Exit", }; Scanner scanner = new Scanner(System.in); int option = 1; while (option!=4){ printMenu(options); try { option = scanner.nextInt(); } catch (InputMismatchException ex){ System.out.println("Please enter an integer value between 1 and " + options.length); scanner.next(); } catch (Exception ex){ System.out.println("An unexpected error happened. Please try again"); scanner.next(); } } } }
Implementing actions for each option of the console menu
After printing the menu, asking the user for an option, and handling possible errors, it is time to act depending on the user input.
package com.RP; import java.util.Scanner; import static java.lang.System.exit; public class Main { public static void printMenu(String[] options){ for (String option : options){ System.out.println(option); } System.out.print("Choose your option : "); } public static void main(String[] args) { String[] options = {"1- Option 1", "2- Option 2", "3- Option 3", "4- Exit", }; Scanner scanner = new Scanner(System.in); int option = 1; while (option!=4){ printMenu(options); try { option = scanner.nextInt(); switch (option){ case 1: option1(); break; case 2: option2(); break; case 3: option3(); break; case 4: exit(0); } } catch (Exception ex){ System.out.println("Please enter an integer value between 1 and " + options.length); scanner.next(); } } } // Options private static void option1() { System.out.println("Thanks for choosing option 1"); } private static void option2() { System.out.println("Thanks for choosing option 2"); } private static void option3() { System.out.println("Thanks for choosing option 3"); } }
From the code above, you will see the use of a switch statement (a conditional statement). You can also use an if statement if you prefer. It will work just in the same way.
Also, we implemented one new method per each option. Within this method, you can write the code you want to execute when the user chooses a specific option.
If you add more options to your menu, just add more cases to the switch statement (case 5, case 5 and so on) and implement one method per each option.
Example of Java console menu for simple calculations
In this example, we want to give the possibility to the user to enter two numbers and then decide what to do with them from two options, sum or multiply them.
Let’s see the code.
package com.RP; import java.util.Scanner; import static java.lang.System.exit; public class JavaConsoleMenuExample { public static void printMenu(String[] options){ for (String option : options){ System.out.println(option); } System.out.print("Choose your option : "); } private static String[] options = {"1- Sum ", "2- Multiply", "3- Exit", }; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first numnber: "); int number1 = scanner.nextInt(); System.out.print("Enter the second numnber: "); int number2 = scanner.nextInt(); int option = 1; while (option!=4){ printMenu(options); try { option = scanner.nextInt(); switch (option){ case 1: sum(number1, number2); break; case 2: multiply(number1, number2); break; case 3: exit(0); } } catch (Exception ex){ System.out.println("Please enter an integer value between 1 and " + options.length); scanner.next(); } } } // Options private static void sum(int number1, int number2) { int result = number1 + number2; System.out.println("The sum is " + result); } private static void multiply(int number1, int number2) { int result = number1 * number2; System.out.println("The result is " + result); } }
For you to keep practising, you can add exception handling to the input of the two numbers. If the user enters a string instead of a number, the app will crash.
See below an output example of the previous code.
Summary
To create a Java Console Menu, we use the following:
- String array to store the options available
- Infinite loop to print the menu until the user chooses to exit the console application
- A switch statement to identify the option the user wants to execute
- A method per option so our code is cleaner and easier to maintain
Related topics:
Leave a comment below.
H@ppy coding!