-2

i am writing a program to provide a menu for the user .. after he enters a number i use switch to decide which parameter should the user enter . Anyway, in one case ( case 1 ) i need to inputs from the user . but after the user enter the first input the program breaks the switch and go to do what is after the switch .

code :

the case 1 :

 case 1:
          System.out.println("Enter the Amount :");
          currentAccount.debit(scanner.nextDouble());

          System.out.println("Want anything else(yes/no)?");
          String input=scanner.nextLine();

          if(input.equalsIgnoreCase("no")){
          isFinished=true;   
          currentAccount=null;
          System.out.println("SignedOut successfully");  
          }
          break;

output:

Choose an opearation: 
1.withdraw.
2.deposit. 
3.transaction history.
  1
Enter the Amount :
  100

 Debit amount exceeded account balance.
 Want anything else(yes/no)?


 --------- Mhd Bank ---------
 logined as : 
 --------------------------------
 Choose an opearation: 
 1.withdraw.
 2.deposit. 
 3.transaction history.
Mhd Ghd
  • 13
  • 1
  • 5

1 Answers1

0

This is happening due to the Scanner behavior after scanner.nextDouble();, the line is not fully consumed.

Try this workaround

case 1:
    System.out.println("Enter the Amount :");
    currentAccount.debit(scanner.nextDouble());
    scanner.nextLine();  // Consume newline left

    System.out.println("Want anything else(yes/no)?");
    String input=scanner.nextLine();

    if(input.equalsIgnoreCase("no")){
        isFinished=true;   
        currentAccount=null;
        System.out.println("SignedOut successfully");  
    }
    break;
Villat
  • 1,354
  • 11
  • 31