Java basic console programming - can use hasNextLine to read inputs from console? -


i have been presented code looks unnatural me. use hasnextline() rather boolean variable done shown in code in while loop, i'm confused. question is, can replace logic variable done shown hasnextline() when input expected console, or can use hasnextline() when input comes file? better practice way of implementing code input comes console, using done variable or hasnextline()? thanks.

// tcpclient.java  import java.net.*; import java.io.*; import java.lang.*;  public class tcpclient{      public static void main(string args[]){     socket clientsock=null;     try{         int port_num = integer.valueof(args[1]).intvalue();  //get server's port no.         clientsock = new socket(args[0],(int)port_num);  // args[0] server host name          /* string sock=clientsock.tostring();         system.out.println(sock); */          printwriter ostream =         new printwriter(clientsock.getoutputstream(),true);          bufferedreader istream =         new bufferedreader(new inputstreamreader                    (clientsock.getinputstream()));          bufferedreader keyinput =         new bufferedreader(new inputstreamreader(system.in));          boolean done = false;         string answer = istream.readline();         if(answer != null)         system.out.println(answer);          while(!done){         string line = keyinput.readline();         if(line.trim().equals("bye"))             done = true;         ostream.println(line);         answer = istream.readline();         if(answer != null)             system.out.println(answer);         }          clientsock.close();     }catch(exception e){         system.out.println(e);     }     } } 

there bug in @voo's solution because nextline() on keyinput return null. here's corrected version:

string line; while ((line = keyinput.readline()) != null &&         !line.trim().equals("bye")) {     ostream.println(line);     answer = istream.readline();     if (answer != null) {         system.out.println(answer);     } } 

can replace logic variable done shown hasnextline() when input expected console, or can use hasnextline() when input comes file?

you can wrap inputstream or readable (of reader subtype) in scanner, allowing use hasnextline() on of them. caveat hasnextline() can potentially block indefinitely waiting input if underlying stream comes console, pipe, socket or similar.

which better practice way of implementing code input comes console, using done variable or hasnextline()?

either do, third option illustrated above. matter of taste ... , think looks simplest. (personally, i'd not use scanner just can call hasnextline() ... that's opinion.)

the other significant difference between using scanner , bufferedreader scanner hides ioexceptions might occur in call hasnext...() , returns false. thing typical use-cases of scanner light-weight user input parser (as using on keyinput), maybe not in other use-cases.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -