• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading 3 lines of text

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I read and print the following-
it is a warm day today.
we can go out for a picnic.
why not go to the beach
using-
BufferedReader bf = new BuferedReader(new InputStreamReader(client.getInputStream());
where client is an object of class Socket.
?
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you can explain your problem in a little more detail, we can provide more assistance. However, this vaguely resembles a homework problem, so don't expect a lot of help until you have attempted to tackle this on your own.
Post your code, and we will try to help out.
Tom Blough
[ February 13, 2004: Message edited by: Tom Blough ]
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is basically regarding readLine().
Incase I use-
(System.out.println(bf.readLine());
will this print only the first line of text coming from the server?
You have suggested I use a for loop.
But why use a for loop? What does the counter ii denote here? Is it the number of sentences?
And what happens if there are more sentences coming from the server, lets say 6 sentences?
Basically, I would like to know how to use readLine to print text which may sometimes be 2, 3 or 6 or any number of lines.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to put your system.out statement into a loop.
Something like this:
String line = "";
While( (line = bf.readLine()) != null ) {
System.out.println(line);
}
Note: This is from a long time ago, don't know if it's fixed yet. readLine() method waits for \n\r from server. Sometime a server only send \n. That causes client to wait or crashed, that's why using readLine() is abit dangerous.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lucky Singh:
What does the counter ii denote here? Is it the number of sentences?
And what happens if there are more sentences coming from the server, lets say 6 sentences?
Basically, I would like to know how to use readLine to print text which may sometimes be 2, 3 or 6 or any number of lines.

ii is indeed the number of sentences. You thread topic asked how to read 3 lines of text, and your example showed 3 lines of text. If you want to read varying numbers of lines of text, then you will need to use a variable to hold the number of lines you wish to read.
If you want to read all of the lines of text in the buffer, that answer has been presented in this thread as well. If your stream is file based, then realLine() will return null when the end of file is reached.
If your stream is coming from stdIn, then you will not get a null because stdIn does not end. In that case, you can stop looping when the length of the line read is 0.
Tom
[ February 13, 2004: Message edited by: Tom Blough ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!