java - End of file NullPointerException -
what wanted reach eof
typing ctrl + z command line bufferedreader
reading console. following code so. problem is, issues nullpointerexception
after reaching eof
. there way skip exception? or more precisely, proper way of reaching eof
bufferedreader
reading console?
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; class eof { public static void main(string args[]) { string s = ""; string eof = "^z"; bufferedreader read = new bufferedreader(new inputstreamreader(system.in)); try { while (!s.equals(eof)) { s = read.readline(); } } catch (ioexception e) {} } }
or more precisely, proper way of reaching eof bufferedreader reading console?
currently you're detecting characters '^' , 'z' it's not '^' control character.
the exception you're getting hint how should handling this. docs bufferedreader.readline:
returns:
string containing contents of line, not including line-termination characters, or null if end of stream has been reached
so should loop until readline
returns null
.
string line; while((line = read.readline()) != null) { // line }
Comments
Post a Comment