The problem is that the input will not be <0 when it reaches the line that returns null, so do:
while (leido.length() != 0) {
....
}
The problem is that the input will not be <0 when it reaches the line that returns null, so do:
while (leido.length() != 0) {
....
}
I am not really sure if I understand your problem correctly. If you merely want your program to stop reading from the console, you can "close" the console by hitting Ctrl+D (Linux) or Ctrl+Z (Windows). This causes System.in.read() to return -1, so that ReadLn method would return null.
Check for null as Nathaniel suggested.
Edit: (based on your comment)
The problem is with this condition in the ReadLn method
if ((input < 0) && (length == 0)) return null;
If you enter a blank line, length will be 0, input will be > 0 though (whatever your system uses to denote a newline). If you change it to
if ((input < 0) || (length == 0)) return null;
the method will return null if either the input stream is closed or a blank line is entered.strong text
Instead of !leido.equals(" ")
, simply use !leido.equals("")
. The former terminates when a line containing only a space is entered, the latter when a blank line is entered.
I dont understand your problem much, but just a suggest from my side will be that use your condition in this way
while(!" ".equals(leido)){
//instead of
while (!leido.equals(" ")){
why ?, because your code will throw a Exception if your variable contains null, and the condition which i mentioned will never do that :P