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

  • SEARCH
  • Home
  • 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 34735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:07:31+00:00 2026-05-10T14:07:31+00:00

I am using the code snippet below, however it’s not working quite as I

  • 0

I am using the code snippet below, however it’s not working quite as I understand it should.

public static void main(String[] args) {     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));      String line;     try {         line = br.readLine();         while(line != null) {             System.out.println(line);             line = br.readLine();         }     } catch (IOException e) {         e.printStackTrace();     } } 

From reading the Javadoc about readLine() it says:

Reads a line of text. A line is considered to be terminated by any one of a line feed (\n), a carriage return (\r), or a carriage return followed immediately by a linefeed.

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Throws: IOException – If an I/O error occurs

From my understanding of this, readLine should return null the first time no input is entered other than a line termination, like \r. However, this code just ends up looping infinitely. After debugging, I have found that instead of null being returned when just a termination character is entered, it actually returns an empty string (”). This doesn’t make sense to me. What am I not understanding correctly?

  • 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. 2026-05-10T14:07:31+00:00Added an answer on May 10, 2026 at 2:07 pm

    From my understanding of this, readLine should return null the first time no input is entered other than a line termination, like ‘\r’.

    That is not correct. readLine will return null if the end of the stream is reached. That is, for example, if you are reading a file, and the file ends, or if you’re reading from a socket and the socket closses.

    But if you’re simply reading the console input, hitting the return key on your keyboard does not constitute an end of stream. It’s simply a character that is returned (\n or \r\n depending on your OS).

    So, if you want to break on both the empty string and the end of line, you should do:

    while (line != null && !line.equals('')) 

    Also, your current program should work as expected if you pipe some file directly into it, like so:

    java -cp . Echo < test.txt 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the obsolete System.Web.Mail sending email works fine, here's the code snippet: Public Shared
I'm using the code snippet below to create a JFormattedTextField. When entering values via
using the Code Snippet for sending email in VB.Net I have successfully sent an
Using the code below, I am returning an nvarchar field from MS SQL 2005
The following code using System.Threading; class Test { volatile int counter = 0; public
In the code HTML+Script below, an event handler is attached after page load using
Anyone have any idea why CGContextFillPath won't work in the code snippet below? I'm
I am trying to add two Vectors below is the code snippet :- #include
The code snippet below search allow the user to match a string against three
Shown below is a snippet of code where I try and reference my ApplicationProperties

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.