Mimi Mimi - 2 months ago 6
Java Question

Java: how do you go back to the previous menu with do while and switch?

I have a do-while loop within a do-while loop and switch cases for each of them.

I want the main menu to be looping forever and want the main menu cases to go back to the main menu after some condition.

For example:

//main menu
do {
case 1:
do {
case 1:
case 2:
} while(something);
//go back to main menu
case 2:
case 3:
case 4:
} while(true);


How may I achieve this in java code?

Thanks a lot for your help!
Below is my code for your reference (should you need it) :)

import java.util.Scanner;

/** Class PriorityQueueTest **/
public class DataStructureTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

do {

System.out.println("[1] Priority Queue");
System.out.println("[2] 2D Array");
System.out.println("[3] 3D Array");
System.out.println("[4] Map");
System.out.println("[5] SortedMap");
System.out.println("[6] HashMap");
System.out.println("[7] TreeMap");
System.out.println("[8] LinkedHashMap");
System.out.println("[9] Set");
System.out.println("[10] SortedSet");
System.out.println("[11] HashSet");
System.out.println("[12] TreeSet");
System.out.println("[13] LinkedHashSet");
System.out.println("[14] List");
System.out.println("[15] ArrayList");
System.out.println("[16] LinkedList");
System.out.println("[17] Stack");

System.out.print("Please choose a data structure: ");

int num = scanner.nextInt();
switch (num) {

// ==================== PriorityQueue ====================
case 1:
System.out.println("\n=== Priority Queue Test ===\n");

System.out.print("Enter size of priority queue: ");
PriorityQueue pq = new PriorityQueue(scanner.nextInt());

char a;
/* Perform Priority Queue operations */
do {
System.out.println("\nPriority Queue Operations\n");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. check empty");
System.out.println("4. check full");
System.out.println("5. empty");
System.out.println("6. check size");
System.out.println("7. print");

int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter job name and priority: ");
pq.insert(scanner.next(), scanner.nextInt());
break;
case 2:
System.out.println("\nJob removed \n\n" + pq.remove());
break;
case 3:
System.out.println("\nEmpty Status: " + pq.isEmpty());
break;
case 4:
System.out.println("\nFull Status: " + pq.isFull());
break;
case 5:
System.out.println("\nPriority Queue Cleared!");
pq.clear();
break;
case 6:
System.out.println("\nSize = " + pq.size());
break;
case 7:
pq.print();
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
System.out.println("\nDo you want to continue? (y or n) \n");
a = scanner.next().charAt(0);
} while (a == 'Y' || a == 'y' || a != 'N' || a != 'n');
break;

// ==================== 2D Array ====================
case 2:
TwoDimensionalArray array = new TwoDimensionalArray();

System.out.println("\n=== 2D Array Test ===\n");
System.out.println("Enter size of 2D Array: ");
int DIM = scanner.nextInt();
int[][] table = new int[DIM][DIM];

char b;
do {

System.out.println("1. insert [x][y] coordinate");
System.out.println("2. remove [x][y] coordinate");
System.out.println("3. empty");
System.out.println("4. print");

int choice = scanner.nextInt();
switch (choice) {
case 1:
array.insert(table, scanner);
break;
case 2:
array.remove(table, scanner);
break;
case 3:
array.empty(DIM);
break;
case 4:
array.print(DIM, table);
break;
}
System.out.println("\nDo you want to continue? (y or n) \n");
b = scanner.next().charAt(0);
} while (b == 'Y' || b == 'y' || b != 'N' || b != 'n');
break;

case 3:
System.out.println("\n=== 3D Array Test ===\n");
break;
case 4:
System.out.println("\n=== Map Test ===\n");
break;
case 5:
System.out.println("\n=== SortedMap Test ===\n");
break;
case 6:
System.out.println("\n=== HashMap Test ===\n");
break;
case 7:
System.out.println("\n=== TreeMap Test ===\n");
break;
case 8:
System.out.println("\n=== LinkedHashMap Test ===\n");
break;
case 9:
System.out.println("\n=== Set Test ===\n");
break;
case 10:
System.out.println("\n=== SortedSet Test ===\n");
break;
case 11:
System.out.println("\n=== HashSet Test ===\n");
break;
case 12:
System.out.println("\n=== TreeSet Test ===\n");
break;
case 13:
System.out.println("\n=== LinkedHashSet Test ===\n");
break;
case 14:
System.out.println("\n=== List Test ===\n");
break;
case 15:
System.out.println("\n=== ArrayList Test ===\n");
break;
case 16:
System.out.println("\n=== LinkedList Test ===\n");
break;
case 17:
System.out.println("\n=== Stack Test ===\n");
break;
default:
System.out.println("Please choose a number on the list!\n");
}
while (num == 1 || num == 2 || num == 3 || num == 4 || num == 5 || num == 6 || num == 7 || num == 8 || num == 9
|| num == 10 || num == 11 || num == 12 || num == 13 || num == 14 || num == 15 || num == 16 || num == 17)
;
} while (true);
}
}

Answer

Take a look at your while conditions.

while (a == 'Y' || a == 'y' || a != 'N' || a != 'n');

This always evaluates to true and so you never the leave the inner loop.

You need to change it to

while ((a == 'Y' || a == 'y') && (a != 'N' || a != 'n'));

This part doesn't seem necessary

  while (num == 1 || num == 2 || num == 3 || num == 4 || num == 5 || num == 6 || num == 7 || num == 8 || num == 9
      || num == 10 || num == 11 || num == 12 || num == 13 || num == 14 || num == 15 || num == 16 || num == 17)
    ;

If you remove it and change the while conditions accordingly it should work.