aspose file tools*
The moose likes Beginning Java and the fly likes BufferedReader not reading first character entered Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BufferedReader not reading first character entered" Watch "BufferedReader not reading first character entered" New topic
Author

BufferedReader not reading first character entered

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

Have a look at the code:



When I provide the input to the stream as "abcdefgh" and press enter key, it prints all the characters except the first one "a". Could anyone tell me why is this happening? Why isn't it reading all the characters in to the character array?


~ Mansukh
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

You while statement reads a character and checks it for end-of-file but then ignores the value read and the body of the while reads the next character in the stream! Note - this means you will print alternate characters and not as you say miss only the first one.

As far as reading characters into the array is concerned, you don't actually put them into the array so your array is not used.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Richard Tookey wrote:As far as reading characters into the array is concerned, you don't actually put them into the array so your array is not used.

Yes he does. On line 10. That also explains why he is only losing the first character and not alternative characters.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Where is the first character going Jo?
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Joanne Neal wrote:
Richard Tookey wrote:As far as reading characters into the array is concerned, you don't actually put them into the array so your array is not used.

Yes he does. On line 10. That also explains why he is only losing the first character and not alternative characters.


:-) Yep - my mistake! Since he reads a character in the while() I made the assumption he read a character at a time in the body of the loop. Silly of me. This does not change the fact that the character read in the while is discarded without being used.
Rommel Sharma
Greenhorn

Joined: Oct 31, 2003
Posts: 18
Mansukhdeep Thind wrote:Where is the first character going Jo?


Assigned to an integer and returned - therefore being compared with an integer. From the java docs, on what is returned:

The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

This does not change the fact that the character read in the while is discarded without being used.
So, how do I ensure that I read the first character also?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Mansukhdeep Thind wrote:
This does not change the fact that the character read in the while is discarded without being used.
So, how do I ensure that I read the first character also?

You need to asign it to a variable in the which statement. It's not obvious how to do this so I'll show you just this once

If you want to read the characters into an array in one go, that's a bit more complicated, so I'll leave that as an exercise for you.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Why this peculiar behavior? What magic happens when I explicitly assign the value returned by the read() method to an int primitive and then read it? What was preventing the earlier code from reading the first character?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Mansukhdeep Thind wrote:What was preventing the earlier code from reading the first character?

The earlier code was reading the first character, but because you didn't assign the read value to any variable it was discarded as soon as the comparison to -1 had been done. You then read all the remaining characters (or at least up to 1024 of them) into the array.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2317
    
  49
It's not peculiar behavior at all. Every time you call read() you consume 1 character from the stream. In your code you read the first char and compare it to -1. Then you read the remaining chars into the char array.

In the code provided by Joanne the value returned by read() is assigned to a char variable which is then compared to -1, so the while loop executes once for each char until the end of the stream is found. If you change your code to only read one char at a time inside the while loop you will see it only prints out every other char. This is because after reading the char and printing it out the while statement would execute again and read the next char and and execute the while loop until the end of stream is found. Of course if there is an odd number of chars in the stream the last print out will print the end of stream marker.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

OK. Thank you Jo and Tony for making me understand the nuances. I shall mark the topic resolved.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BufferedReader not reading first character entered