If you want to know how to create an options menu with the Java console, then stay and read this article where we will show you how to do it easily and quickly.
With this little 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 junk codes and the best of all is that 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 that you keep in mind that these lines of code must be listed in sequence, including the blank lines.
All the steps that we will show you below are done in a main. The first thing you will 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’s option
- while (! exit) {
- }
- }
- }
After this series of commands, you must make it ask for options, and this process is done through while, you can do it with a do-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’s option
- 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 <option number> <Opcion> . With three options it would be like this:
- 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’s option
- while (! exit) {
- 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 (“Write one of the options”);
- option = sn.nextInt ();
- }
- }
- }
It is 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, 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’s option
- while (! exit) {
- 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 (“Write 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”);
- }
- }
- }
- }
What happens if an error appears?
You may have put a string instead of a number and that’s the reason for the error, so for this, you must 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’s option
- while (! exit) {
- 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 (“Write 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 ();
- }
- }
- }
- }
This exception is InputMismatchException, which will be thrown when, if it is the case, the input cannot be converted into a number. You must put sn.next () so that you do not enter an infinite loop if you are using Scanner. And with these codes your menu will be completed.