import java.util.inputmismatchexception; import java.util.scanner; public class bank { double balance = 0; double amount = 0; scanner in = new scanner(system.in); int userchoice; bankaccount account1 = new bankaccount(); boolean quit = false; { { system.out.println("your choice: "); system.out.println("for deposit type 1"); system.out.println("for withdraw type 2"); system.out.println("for check balance type 3"); system.out.println("type 0 quit"); userchoice = in.nextint(); switch (userchoice) { case 1: //deposit money boolean inputinvalid = false; { system.out.println("how deposit?"); try { in.usedelimiter("\n"); amount = in.nextdouble(); inputinvalid = false; } catch(inputmismatchexception ime) { system.out.println("invalid input. try again"); inputinvalid = true; } } while (inputinvalid); system.out.println("depositing: " + amount); account1.deposit(amount); //balance = amount + balance; break; case 2: //withdraw money boolean invalidinput = false; { system.out.println("how withdraw?"); try { in.usedelimiter("\n"); amount = in.nextdouble(); invalidinput = false; } catch(inputmismatchexception ime) { system.out.println("invalid input. try again"); invalidinput = true; } } while (invalidinput); system.out.println("withdrawing: " + amount); account1.withdraw(amount); //balance = balance - amount; break; case 3: //check balance system.out.println("checking balance."); account1.getbalance(); system.out.println(account1.balance); break; case 0: system.out.println("thanks using bankaccount banking system!"); quit = true; break; default: system.out.println("error: choice not recognized please choose again."); continue; } if (userchoice == 0) quit = true; } while (!quit); } }
my code otherwise works fine can't seem figure out why won't stop repeatedly printing error message user. if can point out error me fantastic. did have same code in question fixed problem had in last question , unable answer problem arose here.
you need remove or comment out following line code :
in.usedelimiter("\n");
this causing the character "\n" passed amount = in.nextdouble()
, in turn causes inputmismatchexception thrown , resulting in infinite loop.
update : working code , sample output convinience:
import java.util.inputmismatchexception; import java.util.scanner; public class bank { public static void main(string[] args) { double balance = 0; double amount = 0; @suppresswarnings("resource") scanner in = new scanner(system.in); int userchoice; bankaccount account1 = new bankaccount(); boolean quit = false; { { system.out.println("your choice: "); system.out.println("for deposit type 1"); system.out.println("for withdraw type 2"); system.out.println("for check balance type 3"); system.out.println("type 0 quit"); system.out.print("user input :"); userchoice = in.nextint(); switch (userchoice) { case 1: // deposit money boolean inputinvalid = false; { system.out.println("how deposit?"); try { // in.usedelimiter("\n"); amount = in.nextdouble(); inputinvalid = false; } catch (inputmismatchexception ime) { system.out.println("invalid input. try again"); inputinvalid = true; } } while (inputinvalid); system.out.println("depositing: " + amount); account1.deposit(amount); // balance = amount + balance; break; case 2: // withdraw money boolean invalidinput = false; { system.out.println("how withdraw?"); try { // in.usedelimiter("\n"); amount = in.nextdouble(); invalidinput = false; } catch (inputmismatchexception ime) { system.out.println("invalid input. try again"); invalidinput = true; } } while (invalidinput); system.out.println("withdrawing: " + amount); account1.withdraw(amount); // balance = balance - amount; break; case 3: // check balance system.out.println("checking balance."); account1.getbalance(); system.out.println("available balance : " + account1.getbalance()); break; case 0: system.out.println("thanks using bankaccount banking system!"); quit = true; break; default: system.out.println("error: choice not recognized please choose again."); continue; } if (userchoice == 0) quit = true; } while (!quit); } } }
sample output :
your choice: deposit type 1 withdraw type 2 check balance type 3 type 0 quit user input :1 how deposit? 100 depositing: 100.0 choice: deposit type 1 withdraw type 2 check balance type 3 type 0 quit user input :25 error: choice not recognized please choose again. choice: deposit type 1 withdraw type 2 check balance type 3 type 0 quit user input :2 how withdraw? 25 withdrawing: 25.0 choice: deposit type 1 withdraw type 2 check balance type 3 type 0 quit user input :3 checking balance. available balance : 75.0 choice: deposit type 1 withdraw type 2 check balance type 3 type 0 quit user input :0 using bankaccount banking system!