2 Replies - 488 Views - Last Post: 02 March 2013 - 10:17 AM Rate Topic: -----

#1 LatifaHR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-February 13

How can I print a file in the reverse order?

Posted 02 March 2013 - 02:59 AM

I have a text file and i need to print the lines in the reverse order

THE QUESTION: If the file has more than five lines, it will print ONLY the first five lines in the reverse order. If the file has less than five lines then these should be displayed as it appears in the file (NOT reversed).


This is the text File
Line #one
Line #two 
Line #three
Line #four
Line #five


Is This A Good Question/Topic? 0
  • +

Replies To: How can I print a file in the reverse order?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How can I print a file in the reverse order?

Posted 02 March 2013 - 03:09 AM

Your question looks like a statement, but it sets up a nice 'if' statement that describes what you need to program: attempt to read the first 6 lines from the file, if 6 lines are read, output 5 lines in reverse order, otherwise output the number of lines read in original order.

Can you use arrays?
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: How can I print a file in the reverse order?

Posted 02 March 2013 - 10:17 AM

In order to print it in reverse order, you're going to first have to read it in proper order. Just grab the lines and put them into a String[]. If it's size is > 5, print the array backwards. If not, do it forwards.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1