File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic

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

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Joanne is right. Let’s lock this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic