Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read in N Lines of an Input Stream and print in reverse order without using array or list type structure?

Using the readLine() method of BufferedReader, can you print the first N lines of a stream in reverse order without using a list or an array?

like image 508
algorithmicCoder Avatar asked Jun 25 '11 20:06

algorithmicCoder


1 Answers

I think you can do it through recursion with something like:

void printReversed(int n)
{
   String line = reader.readLine();

   if (n > 0)
     printReversed(n-1);

   System.out.println(line);
}
like image 131
Jack Avatar answered Oct 05 '22 22:10

Jack