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 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

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It will depend on your purpose. If interoperability with other… May 11, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer Add an outlet to FlipsideViewController.h for the UINavigationBar. Open the… May 11, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer Use GetAncestor() with the GA_PARENT flag. This will retrieve a… May 11, 2026 at 8:22 pm

Related Questions

I am using the code snippet below, however it's not working quite as I
I am trying to disable a number of text boxes intended for displaying data
I am having trouble understanding how the System Registry can help me convert a
I am implementing Code Generation for WindowsForm control at Design-Time, using a Custom CodeDomSerializer.
The array has lots of data and I need to delete two elements. Below

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.