If you want to know how to create an options menu with the Java console, then keep reading this article where we will show you how to do it easily and quickly.
With this small guide you can learn to create your own projects. You will be able to create this Java menu, this method will help you not to generate garbage code and best of all, you will create this menu from scratch. That is why it is important to download, update and install Java to the latest version. It is also important to keep in mind that these lines of code must be numbered in sequence, including blank lines.
How to make a menu of options with Do While in Java
The repetitive Do While statement is extremely important for implementing various functions. Among the most common uses is making a menu, video games, performing recursive calculations, among many other uses.
Well, this statement is very similar to While. But in this case, the Do While executes the loop at the beginning and then evaluates the condition. This makes it mandatory for the loop to execute at least once.
All the steps that we will show you below are done in a main. The first thing you’ll need is a boolean, a numeric variable, and a Scanner.
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main(String[] args) {
- Scanner sn = new Scanner(System.in);
- boolean exit = false;
- int option; // We will save the user option
- do{
- }
- while(!exit);
- }
- }
After this series of commands, you must make it ask for options, and this process is done through Do While, you can do it with a While.
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main(String[] args) {
- Scanner sn = new Scanner(System.in);
- boolean exit = false;
- int option; // We will save the user option
- do{
- }
- while(!exit);
- }
- }
Thanks to this, you will not be able to exit unless you put true. Now you can place the options you want. It is recommended that you use System.out.println and with this format
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main(String[] args) {
- Scanner sn = new Scanner(System.in);
- boolean exit = false;
- int option; // We will save the user option
- do{
- System.out.println(“1. Option 1”);
- System.out.println(“2. Option 2”);
- System.out.println(“3. Option 3”);
- System.out.println(“4. Exit”);
- System.out.println(“Enter one of the options”);
- option = sn.nextInt();
- }
- while(!exit);
- }
- }
It’s time to use a switch to be able to perform the tasks you need. In each case you must put the code you need, and in the last one you must put exit=true and thus indicate that you want to exit. You must also place a default and thus avoid checking that the number is correctly placed in the range of options:
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main(String[] args) {
- Scanner sn = new Scanner(System.in);
- boolean exit = false;
- int option; // We will save the user option
- do{
- System.out.println(“1. Option 1”);
- System.out.println(“2. Option 2”);
- System.out.println(“3. Option 3”);
- System.out.println(“4. Exit”);
- System.out.println(“Enter one of the options”);
- option = sn.nextInt();
- switch(option) {
- Case 1:
- System.out.println(“You have selected option 1”);
- break;
- Case 2:
- System.out.println(“You have selected option 2”);
- break;
- Case 3:
- System.out.println(“You have selected option 3”);
- break;
- Case 4:
- exit=true;
- break;
- default:
- System.out.println(“Only numbers between 1 and 4”);
- }
- }
- while(!exit);
- }
- }
Learn to use the options menu with Do While in Java
To use the Do While menu, you must enter the digit corresponding to the option you want to choose and press Enter. In this way, your option will be processed and executed. In case of typing a number outside the range of options, the program will show you an alert about the available range.
The Do While menu is very easy to use and implement. However, two considerations must be taken into account:
Reassign the output variable: You should never do without an output option in your menu. Otherwise, users will be forced to choose an option without being able to exit. Similarly, this option should reassign the value of the output variable to break the Do While loop.
Correctly locate the braces: In case your menu is made up of many more structured statements; you must maintain a correct margin with the dependencies. Otherwise, it is difficult to determine which key belongs to the Do While loop.
How to display multiple options within a menu in Java?
The display of the options is executed when entering the Do While loop. In this way, we can show the user what it can do and then execute the option they choose.
This deployment is done sequentially. Therefore, all the options will be shown to the user at the same time.
The code to indicate the menu options is as follows:
- System.out.println(“1. Choice 1”);
- System.out.println(“2. Option 2”);
- System.out.println(“3. Choice 3”);
- System.out.println(“4. Exit”);
What happens if I get an error?
You may have put a string instead of a number and that is the reason for the error, so for this you have to put this special exception.
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main(String[] args) {
- Scanner sn = new Scanner(System.in);
- boolean exit = false;
- int option; // We will save the user option
- do{
- System.out.println(“1. Option 1”);
- System.out.println(“2. Option 2”);
- System.out.println(“3. Option 3”);
- System.out.println(“4. Exit”);
- try {
- System.out.println(“Enter one of the options”);
- option = sn.nextInt();
- switch (option) {
- Case 1:
- System.out.println(“You have selected option 1”);
- break;
- Case 2:
- System.out.println(“You have selected option 2”);
- break;
- Case 3:
- System.out.println(“You have selected option 3”);
- break;
- Case 4:
- exit = true;
- break;
- default:
- System.out.println(“Only numbers between 1 and 4”);
- }
- } catch (InputMismatchException e) {
- System.out.println(“You must insert a number”);
- sn.next();
- }
- }
- while (!exit) ;
- }
- }
This exception is InputMismatchException, which will be thrown when, if it is the case, the input cannot be converted to a number. You should put sn.next() so you don’t get into an infinite loop if you’re using Scanner. And with these codes your menu will be complete.