debugging - Java: try(Scanner scan = new Scanner(System.in) { } causing an exception -
using try(scanner scan = new scanner(system.in)) { }
causing
exception in thread "main" java.util.nosuchelementexception
when try debug says
variable information not available, source compiled without -g option.
and shows below code
public scanner(inputstream source) { this(new inputstreamreader(source), whitespace_pattern); }
one of methods uses line:
protected string loginname(){ string username; string password; try (scanner scan = new scanner(system.in)) { // line causing error. system.out.print("enter username: "); username = scan.next(); system.out.print("enter password: "); password = scan.next(); } if(getusernameslist().contains(username)) if(password.equals(getpasswordslist().get(getusernameslist().indexof(username)))) return username; else return "-1"; else return "-1"; }
you're closing system.in
(a global-variable). please, not that. everywhere have
try(scanner scan = new scanner(system.in))
guarantees system.in
close
(d). once it's close
(d) can't read again (or mentioned exception
). also, can compile debug symbols (or step ide's built-in debugger or jdb applicable). scanner.close()
javadoc says (in part),
if scanner has not yet been closed if underlying readable implements
closeable
interface readable'sclose
method invoked
Comments
Post a Comment