Friday, 15 June 2012

java.util.scanner - Java Using Scanner Multiple Times -



java.util.scanner - Java Using Scanner Multiple Times -

i having problem lot. when utilize scanner lot of times, doesn't input user.

scanner scan = new scanner(system.in); system.out.println("1---"); int try1 = scan.nextint(); system.out.println("2---"); int try2 = scan.nextint(); system.out.println("3---"); string try3 = scan.nextline(); system.out.println("4---"); string try4 = scan.nextline();

when run code, result :

1---

12

2---

321

3---

4---

aa

as can see, skipped @ 3rd input. why happening? solve problem using new scanners, have 5-6 different scanners , looks complicated. problem : there error "resource leak: scan never closed". confused.

use next api rather nextline when nextint presee come in , generates number + \n , nextint going take integer , wont take \n in turn gets passed next input i.e. try3 why gets skipped.

scanner scan = new scanner(system.in); system.out.println("1---"); int try1 = scan.nextint(); system.out.println("2---"); int try2 = scan.nextint(); system.out.println("3---"); string try3 = scan.next(); system.out.println("4---"); string try4 = scan.next();

java java.util.scanner

No comments:

Post a Comment