• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YOU SPELLED HIS NAME WRONG!!!
 
Allion Salvador
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need a third variable penulitmatePenultimate
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie,
Your post was moved to a new topic.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic