aspose file tools*
The moose likes Beginning Java and the fly likes Why am I losing the first character from a line read with a buffered reader? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Why am I losing the first character from a line read with a buffered reader?

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Change your c variable to an int and take out the char cast on line 12 and then see what gets printed. Does that answer your question ?


This too won't work Jo. Reason being that it will still keep on looping infinitely even after the end of stream is reached. For properly ending(after you press Ctrl+z), you ought to add a check like

. Secondly, even after getting past this problem, the output prints alternate characters. For example, if I entered "abcdefgh" and pressed enter, it will print "bdfh" or corresponding ASCII codes(98100102104). It does not print all of them. So, I modified the code to look something like :



I you give the input to the stream as "abcdefgh" and press enter key, it prints "bcdefgh" i.e. all except the first character "a". Any clue why?


~ Mansukh
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Mansukhdeep Thind wrote:
Change your c variable to an int and take out the char cast on line 12 and then see what gets printed. Does that answer your question ?


This too won't work Jo. Reason being that it will still keep on looping infinitely even after the end of stream is reached. For properly ending(after you press Ctrl+z), you ought to add a check like

But that isn't what the OP is asking. (S)he is asking why there are two blank lines between each block of output. By making the changes I suggest, the reason for these will become more obvious.


Joanne
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

OK. But then can I ask you this although I am not the opener? Why is the first character not being read into the character array?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Mansukhdeep Thind wrote:OK. But then can I ask you this although I am not the opener? Why is the first character not being read into the character array?

You need to either open a new thread to ask that or at least wait until the OP's original question has been answered before hijacking this thread with an unrelated question. If people start discussing your question here, it may cause the original question to go unanswered.
This applies especially here because the OP's code doesn't have that problem.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Mansukhdeep started a new thread before Campbell split this off from the original thread, so it's probably best to respond to that before this gets any more confusing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Joanne is right. Let’s lock this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why am I losing the first character from a line read with a buffered reader?