• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding BufferedReader and related output  RSS feed

 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This program is working fine.
Though I need certain clarifications on its output regarding.

NOTE:-- please Do not type q than only my problem regarding the output will be clear.

if i give the input as 123 and press enter it display as
1
2
3



continiously again if i give input as abcd it display
a
b
c
d


There comes too much space after printing the output why it is so? i am unable to understand?



//Use a BufferedReader to read charcters from the console.
import java.io.*;
class BRRead {
public static void main(String[]args)
throws IOException
{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
//read charcters
do{
c = (char) br.read();
System.out.println(c);

}while(c!='q');

}
}
 
M Mohile
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(c);
means print new line. If you do not want to go to the next line, you can always go for System.out.print();.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mohile thanks for looking into the problem

Yes, System.out.println(c); is for the newline it goes down just for a single line
but here it is taking lots of space i mean upto 2to3 lines

give input as abc press enter and than give input as sgt or any thing you want instead of q

than you will see the out put as
a
b
c

//this is the space i am asking for
sgt
s
g
t

 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh

Always use code tags for pasting code:



Having said that, the program is not working corrcetly. It goes into an infinite loop and never terminates. Can you tell me why?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jdinesh Tiwari wrote:but here it is taking lots of space i mean upto 2to3 lines

give input as abc press enter and than give input as sgt or any thing you want instead of q

than you will see the out put as
a
b
c

//this is the space i am asking for
sgt
s
g
t


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 ?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind,
Your post was moved to a new topic.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mansukhdeep.
I will use code tag
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!