Java basic console programming - can use hasNextLi

2019-08-18 03:07发布

I have been presented with this code which looks unnatural to me. I would normally use hasNextLine() rather than a Boolean variable done as shown in this code in the while loop, but now I'm confused. My question is, can I replace the logic where the variable done is shown with hasNextLine() when input is expected from the console, or can I only use hasNextLine() when input comes from a file? Which is a better practice way of implementing this code where input comes from console, by using the 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] is the 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);
    }
    }
}

2条回答
2楼-- · 2019-08-18 03:37

There is a bug in @Voo's solution because the nextLine() on keyInput could also return null. Here's a 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 I replace the logic where the variable done is shown with hasNextLine() when input is expected from the console, or can I only use hasNextLine() when input comes from a file?

You can wrap any InputStream or any Readable (of which Reader is a subtype) in a Scanner, allowing you to use hasNextLine() on all of them. The only caveat is that hasNextLine() can potentially block indefinitely waiting for input if the underlying stream comes from a console, pipe, socket or similar.

Which is a better practice way of implementing this code where input comes from console, by using the done variable or hasNextLine()?

Either will do, as will the third option as illustrated above. It is really a matter of taste ... and what you think looks simplest. (Personally, I'd not use Scanner just so that I can call hasNextLine() ... but that's just my opinion.)

The other significant difference between using Scanner and BufferedReader is that Scanner hides any IOExceptions that might occur in a call to hasNext...() and returns simply false. This is a good thing for typical use-cases of Scanner as a light-weight user input parser (as you are using it on keyInput), but maybe not in other use-cases.

查看更多
可以哭但决不认输i
3楼-- · 2019-08-18 03:42

Well your idea and the given solution implement different things. The given code stays in the loop until you get "BYE" from the input stream. Your solution would at first need a scanner or your own implementation of hasNextLine() anyhow, since BufferedReader doesn't have one afaik.

But if it's only about using a boolean variable or not, well the alternative would be:

    String line;
    while(!(line = keyInput.readLine()).trim().equals("BYE")){
    oStream.println(line);
    answer = iStream.readLine();
    if(answer != null)
        System.out.println(answer);
    }

I can understand people who don't like assignments in other statements, but I've written similar code with checking the variable for != NULL or EOF, because I found it better readable. I wouldn't make it much more complicated than what you've got right now though and I'd probably go with the boolean variable in that case.

查看更多
登录 后发表回答