Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading the last (or last but 1) line in a text file

 
Campbell Ritchie
Sheriff
Posts: 48436
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an easy way to skip lines in a text file?

I have a text file which reads a bit like this, where %n means newline, and most Britons will recognise AB123456C as a National Insurance number:-

0 1 2 3 4 Campbell Ritchie AB123456C%n
0 12 23 34 45 Campbell Ritchie AB123456C%n
0 123 234 345 456 Campbell Ritchie AB123456C%n
0 1234 2345 3456 4567 Campbell Ritchie AB123456C%n

. . . and I can easily get a java.util.Scanner to read the lines.

But, what I really want is to get it to skip the 1st 2nd and 3rd lines and read the 4th line beginning 0 1234. I have tried all sorts of combinations of while(myScanner.hasNext()){}, while(myScanner.hasNextLine()), myScanner.nextInt(), myScanner.next(), and it happily reads either one line or all five lines (the %n at the end of the 4th line means there is a 5th line with nothing in).

I might use two Scanners, use one to go

. . . but I think there must be a more elegant way to do it.
I don't want to go back to the old J1.4 way of using FileStreams or whatever they were called, because then I have to call Integer.parseInt() for each of the numbers, and scanner2.nextInt() is so much neater.

Any suggestions???
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't recognize the National Insurance Number, but then again cricket doesn't make sense to me either.
Still, this may be what you're looking for:
 
Jody Brown
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A National Insurance Number is the U.K. equivalent of a Social Security Number in the U.S.
 
Campbell Ritchie
Sheriff
Posts: 48436
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that probably is what I am looking for, Mr Albreachtsen.
It is obvious once you think of it.

Thank you.
 
Allion Salvador
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YOU SPELLED HIS NAME WRONG!!!
 
Allion Salvador
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and they are called "FileInputStream" and I think they are fine!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Allion Salvador:
YOU SPELLED HIS NAME WRONG!!!


That's OK. It's only my nom de guerre.
 
Ricardo Wagemaker
Ranch Hand
Posts: 30
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Albertson wrote:I didn't recognize the National Insurance Number, but then again cricket doesn't make sense to me either.
Still, this may be what you're looking for:


Hi Jeff,

Old thread but this is exacly what I wanted, except one small detail and I can't figure it out and that is how do I get the 3rd from last line also.

I have before = penultimate and then penultimate = scanner.nextLine(); also but it does not work :-(

Please help if you are still about.

Many Thanks
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need a third variable penulitmatePenultimate
 
Campbell Ritchie
Sheriff
Posts: 48436
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie,
Your post was moved to a new topic.
 
Campbell Ritchie
Sheriff
Posts: 48436
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Campbell Ritchie,
Your post was moved to a new topic.
Talking to yourself again? Sure sign of insanity.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic