java.util.scanner - How do I close a scanner in java? -
when ever run following code,
private static string input(string message){ scanner input_scanner = new scanner(system.in); system.out.print("\n" + message); string string = input_scanner.nextline(); input_scanner.close(); return string; }
i error:
exception in thread "main" java.util.nosuchelementexception: no line found @ java.util.scanner.nextline(unknown source) @ main.learn.input(learn.java:25) @ main.learn.main(learn.java:13)
i figured out line input_scanner.close();
when remove it, warnings saying:
resource leak: "input_scanner" never closed"
is there anyway can stop errors happening , rid of warnings?
you should check if have data consume scanner using hasnextline
api like:
string string = ""; if (input_scanner.hasnextline()) { string = input_scanner.nextline(); }
Comments
Post a Comment