Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing from a file  RSS feed

 
Jeff Savage
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment includes this step but i'm not sure how to approach it:

"It should read the file and print the next 24 lines and then wait for the user to type any character. When a character is read from input, the program should read and print 24 more lines."

I have the following code so far:

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you need to read the file line by line, it suggests it is a text (rather than a binary) file. So you are better off to use a FileReader rather than a FileInputStream. If you also wrap the FileReader in a BufferedReader, you will see that the BufferedReader has a readLine() method.
Set up a loop to call the readLine method 24 times. You will also need to check for the end of the file in case you reach it before reading 24 lines.
Use the Scanner class to check for the user typing a character.
If you put all of this in to a do/while loop, it should do what you need.

Have a go and if you have any problems, come back with your latest code.
 
Jeff Savage
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what you are saying is where I have:

to change it to be:

?

From there, I'm kind of lost. I know I need to write a while loop to read and write each of the lines, but i'm not sure how to access the readLine() method.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!