Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3975644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:42:35+00:00 2026-05-20T04:42:35+00:00

I have been presented with this code which looks unnatural to me. I would

  • 0

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);
    }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:42:35+00:00Added an answer on May 20, 2026 at 4:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.