java - Reading keyboard input inside while loop -
this question has answer here:
im trying simple menu system, giving me "nosuchelementexception".
when press option 1, executes well, when call option 2 asks me "w" , exception thrown.
i've tried adding if (sc.hasnext)
saw on post, after call option 2, keeps looping , doesn't let me type value x.
public main() { int x = 5; scanner sc = new scanner(system.in); while(x != 0) { system.out.println("########## mt ###########"); system.out.println("# 1 - read mt #"); system.out.println("# 2 - read w #"); system.out.println("# 0 - exit #"); system.out.println("#########################"); system.out.print("option: "); x = sc.nextint(); //here error. switch (x) { case 1: setmaquina(new mt()); break; case 2: if (getmaquina() == null) { system.out.println("mt null."); } else { scanner scc = new scanner(system.in); system.out.print(" ~ w: "); string w = scc.nextline(); getmaquina().readstring(w); scc.close(); } break; case 0: x = 0; break; default: break; } } sc.close(); }
edit
exception in thread "main" java.util.nosuchelementexception @ java.util.scanner.throwfor(scanner.java:862) @ java.util.scanner.next(scanner.java:1485) @ java.util.scanner.nextint(scanner.java:2117) @ java.util.scanner.nextint(scanner.java:2076) @ maquina.turing.main.<init>(main.java:26) @ maquina.turing.main.main(main.java:11)
Comments
Post a Comment