• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BufferedReader not reading first character entered

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the first character going Jo?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Thank you Jo and Tony for making me understand the nuances. I shall mark the topic resolved.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic