Making A Menu Of Options To Do While In Java Step

logo java white background

If you want to know how to create a menu of options with Java console then stick to read this article where we will show how to do it easily and quickly.

With this little guide will learn how to create your own projects. You can create this menu Java, this method will help you do not generate waste codes , and best of all is that this menu will create from scratch. So it is important to download, update and install Java to the latest version . It is also important to have in mind that these lines of code must be listed in sequence , including blank lines.

How To Options menu to Do While in Java step

How to create a menu of options?

All the steps we will show below are facts on a main . The first thing you need is a boolean, numeric variable and a Scanner.

  • import java.util.Scanner;
  • {public class JavaApplication219
  •         public static void main (String [] args) {
  •               sn = new Scanner Scanner (System.in);
  •               boolean exit = false;
  •               int option;//we will keep the option of the user
  •               while (! Out) {
  •             &Nbsp;}
  •       &Nbsp;}
  • }

     illustration of man looking dark background monitor

After this series of commands, you make it go asking for options, and this process is done through while you can make do with a while.

  • import java.util.Scanner;
  • {public class JavaApplication219
  •         public static void main (String [] args) {
  •               sn = new Scanner Scanner (System.in);
  •               boolean exit = false;
  •               int option;//we will keep the option of the user
  •               while (! Out) {
  •             &Nbsp;}
  •       &Nbsp;}
  • }

Thanks to this, you can not go out unless you put true. Now you can put the options you want. It is recommended that you use System.out.println with this format & lt; number option & gt; & lt; Options & gt; . With three options would be this:

  • import java.util.Scanner;
  • {public class JavaApplication219
  •         public static void main (String [] args) {
  •               sn = new Scanner Scanner (System.in);
  •               boolean exit = false;
  •               int option;//we will keep the option of the user
  •               while (! Out) {
  •                       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 ( “Type one of the options’);
  •                       option = sn.nextInt ();
  •             &Nbsp;}
  •       &Nbsp;}
  • }

It’s time to use a switch to perform the tasks you need. In each case you put the code you need, and the last should put out = true and thus indicate that want to leave. Also you place a default and thus avoid being reviewing the number is set correctly in the range of options:

  • import java.util.Scanner;
  • {public class JavaApplication219
  •         public static void main (String [] args) {
  •               sn = new Scanner Scanner (System.in);
  •               boolean exit = false;
  •               int option;//we will keep the option of the user
  •               while (! Out) {
  •                       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 ( “Type one of the options’);
  •                       option = sn.nextInt ();
  •                       switch (choice) {
  •                               case 1:
  •                                       System.out.println ( “You’ve selected option 1 “);
  •                                       break;
  •                               case 2:
  •                                       System.out.println ( “You’ve selected option 2 “);
  •                                       break;
  •                                 case 3:
  •                                       System.out.println ( “You’ve selected option 3 “);
  •                                       break;
  •                                 case 4:
  •                                       out = true;
  •                                       break;
  •                                 default:
  •                                       System.out.println ( “Only numbers between 1 and 4 ‘);
  •                     &Nbsp;}
  •             &Nbsp;}
  •       &Nbsp;}
  • }

What if I get an error?

 laptop monitor with java program

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) {
  •                 sn = new Scanner Scanner (System.in);
  •                 boolean exit = false;
  •                 int option;//we will keep the option of the user
  •                 while (! Out) {
  •                         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 ( “Type one of the options’);
  •                                 option = sn.nextInt ();
  •                                 switch (choice) {
  •                                         case 1:
  •                                               & nbsp ; System.out.println ( “you have chosen option 1”);
  •                                               & nbsp ; break;
  •                                         case 2:
  •                                               & nbsp ; System.out.println ( “you have chosen option 2”);
  •                                               & nbsp ; break;
  •                                         case 3:
  •                                               & nbsp ; System.out.println ( “you have selected option 3”);
  •                                               & nbsp ; break;
  •                                         case 4:
  •                                               & nbsp ; out = true;
  •                                               & nbsp ; break;
  •                                         default:
  •                                               & nbsp ; System.out.println ( “Only numbers between 1 and 4 ‘);
  •                               &Nbsp;}
  •                       &Nbsp;} catch (InputMismatchException e) {
  •                                 System.out.println ( “You must insert a number”);
  •                                 sn.next ();
  •                       &Nbsp;}
  •               &Nbsp;}
  •       &Nbsp;}
  • }

This exception is InputMismatchException, which will jump when, if applicable, introduced what can not be converted to a number. You must put sn.next () lest you enter into an infinite loop if you are using Scanner. And with these codes and your menu will be completed.

Deja un comentario