Luka Luka - 21 days ago 5
Java Question

Java execution flow(in loop) freezes after no text in Scanner

I have scanner(on client) that is getting information from(server), when server sends all required strings, all the code after that is just not being executed(like it paused execution of the program).
Example(it loops and prints good, until server is sending all the data, but, when its finished, it does not execute print, or any other command, even in the loop itself!):

while(scanner1.hasNextLine()){
System.out.print("-- " + scanner1.nextLine());
}
System.out.print("Something bla bla");


RESULT:
-- data 1
-- data 2

Btw, i am new in Java, so if I made any stupid mistakes, I apologize :)

Answer

The program is waiting for the remote to either send an end of line marker (e.g. '\n'), or close its socket output stream. However it seems like the server doesn't send any of them. So instead of using Scanner here you should try using something like client.getInputStream().read()

Comments