Below I have created a Method to iterate through a menu using a do while loop upon obtaining input from a user.
/**
* Method to iterate through a menu using a do while loop.
*/
public static void menuMethodDoWhile(){
// create the scanner class
Scanner menuScanner = new Scanner(System.in);
// declare my variable and assign a value to it
int menuOption = 0;
// try catch finally blocks to handle errors
try {
// do while loop to print a menu to the screen and request input from the user
do {
// try catch blocks to handle user input errors
try {
// menu to be displayed
System.out.println("Menu");
System.out.println("1. File");
System.out.println("2. Edit");
System.out.println("3. Save");
System.out.println("4. Delete");
System.out.println("5. Exit");
System.out.println("Select option.");
// request input from the user
menuOption = menuScanner.nextInt();
// if else statements to determine the user's answer
if (menuOption == 1){
System.out.println("You have selected File.");
} else if (menuOption == 2){
System.out.println("You have selected Edit.");
} else if (menuOption == 3){
System.out.println("You have selected Save.");
} else if (menuOption == 4){
System.out.println("You have selected Delete.");
} else if (menuOption == 5){
System.out.println("You have selected Exit.");
}
} catch (InputMismatchException inputMismatchException){
// catch block
menuScanner.nextLine();
System.out.println("Numbers only please.");
}
} while (menuOption != 5);
} catch (Exception exception){
// catch block
System.out.println("Something went wrong.");
} finally {
// finally block
System.out.println("Exiting program.");
menuScanner.close();
}
}
I was just wondering if it is possible to shorten this code by assigning each menu option to a 2D array and iterating through it using a nested for loop, and if so, how I would go about doing this. So far, all I have is this.
/**
* Method to iterate through a menu using a 2D array and a nested for loop.
*/
public static void menuMethodArrayFor(){
// create the scanner class
Scanner menuScanner = new Scanner(System.in);
// request input from the user
System.out.println("Select option.");
String menuOption = menuScanner.nextLine();
// declare my 2d array
String[][] my2dArray = new String[5][1];
// nested for loop to create, populate and print my2dArray
for (int row = 0; row < my2dArray.length; row++){ // number of rows in my2dArray
for (int column = 0; column < my2dArray[row].length; column++){ // number of columns in my2dArray
System.out.print(my2dArray[row][column]+"\t");
}
System.out.println();
}
}
Declare this array:
String[] entries = new String[]{ "File",..."Exit" };
and use it like this:
do {
try {
// menu to be displayed
System.out.println("Menu");
for( int i = 0; i < entries.length; ++i ){
System.out.println((i+1) + "." + entries[i] );
}
System.out.println("Select option.");
// request input from the user
menuOption = menuScanner.nextInt();
// if else statements to determine the user's answer
if( 1 <= menuOption && menuOption <= entries.length ){
System.out.println("You have selected " + entries.[menuOptions-1] );
} else {
System.out.println("Invalid selection" );
}
} catch (InputMismatchException inputMismatchException){
// catch block
menuScanner.nextLine();
System.out.println("Numbers only please.");
}
} while (menuOption != 5);
There is no need for a two-dimensional array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.