Question

How do I make the system to prompt for another account ID again once I exit the menu by chossing "4: Exit"

        Scanner input = new Scanner(System.in);
        System.out.print("Enter an id: ");
        int ID = input.nextInt();


        System.out.println("\nMain Menu ");
        System.out.println("1. Check balance");
        System.out.println("2. Withdraw");
        System.out.println("3. Deposit");
        System.out.println("4. Exit");
        System.out.println("Enter a choice: ");
        int choice = input.nextInt();

    switch (choice ) {  

        case 1: 
        System.out.println("The balance is: " + accountArray[ID].getBalance());
        break;
        case 2: 
        System.out.println("Enter amount to withdraw: ");
        double withdrawAmount = input.nextDouble();
        accountArray[ID].withdraw(withdrawAmount);
        break;
        case 3: 
        System.out.println("Enter amount to deposit: ");
        double depositAmount = input.nextDouble();
        accountArray[ID].deposit(depositAmount);
        break; 
        case 4: 
        System.out.println("Thank you for banking with us");    
        break;
        }

System.out.println("The Balance is: " + accountArray[ID].getBalance()); }

No correct solution

OTHER TIPS

Surround everything with a while(true) (maybe keep the scanner declaration outside, since it will always be the same).

Since you want the user to be able to continue to query other accounts, nest the loop so when they enter '4' they return to the outer loop which prompts them for a new ID:

    Scanner input = new Scanner(System.in);
    int ID;
    while (true) {
      System.out.print("Enter an id: ");
      ID = input.nextInt();

      int choice = 0;
      while (chioce != 4) {
        System.out.println("\nMain Menu ");
        System.out.println("1. Check balance");
        System.out.println("2. Withdraw");
        System.out.println("3. Deposit");
        System.out.println("4. Exit");
        System.out.println("Enter a choice: ");
        choice = input.nextInt();

        switch (choice ) {  
           case 1: 
           System.out.println("The balance is: " + accountArray[ID].getBalance());
           break;
           case 2: 
           System.out.println("Enter amount to withdraw: ");
           double withdrawAmount = input.nextDouble();
           accountArray[ID].withdraw(withdrawAmount);
           break;
           case 3: 
           System.out.println("Enter amount to deposit: ");
           double depositAmount = input.nextDouble();
           accountArray[ID].deposit(depositAmount);
           break; 
           case 4: 
           System.out.println("Thank you for banking with us");    
           break;
        }

        System.out.println("The Balance is: " + accountArray[ID].getBalance());
      }
    }

If the you want the user to able to terminate the program, change the outer loop condition from true to something similar to the inner loop and prompt them accordingly i.e. 'Enter an id or -1 to exit'.

You can use a while loop:

int choice = 0;

while (choice != 4) {
    System.out.print("Enter an id: ");
    int ID = input.nextInt();
    ...
    int choice = input.nextInt();
    ...
}

Edit:

int choice = 0;

while (true) {
    System.out.print("Enter an id: ");
    int ID = input.nextInt();

    while(choice != 4) {
        ...
        int choice = input.nextInt();
        ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top