i have problem try/catch statement. know it's easy fix i'm new java. here want console prompt user add initial deposit, example, if input "hello" program crash rather ask again? thanks
here errors i'm receiving: exception in thread "main" java.util.inputmismatchexception @
java.util.scanner.throwfor(unknown source)
at java.util.scanner.next(unknown source)
at java.util.scanner.nextint(unknown source)
at java.util.scanner.nextint(unknown source)
system.out.println("please enter initial deposit:"); try{ deposit.add(keyboard.nextint()); }catch(numberformatexception e){ system.out.println("invalid input"); system.out.println("please enter account number:"); accountnumber.add(keyboard.nextdouble());
according docs
method nextint throws inputmismatchexception not numberformatexception
write:
catch(inputmismatchexception e){ ....
update: use snippet , inputing number works
public static void main(string[] args) { scanner keyboard = new scanner(system.in); system.out.println("please enter initial deposit:"); try { int numberentered = keyboard.nextint(); system.out.println(numberentered); } catch (inputmismatchexception e) { system.out.println("invalid input"); system.out.println("please enter account number:"); } { keyboard.close(); } }
console:
please enter initial deposit: 55 55 process finished exit code 0
Comments
Post a Comment