• 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 from the 8th line.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello there, i haf a file which has entries like:



i've already split the string at the "=" into two arrays, the string array and float array. How do i actually start reading the file from 8th line? i dont want to start reading from the first line. Any help will be appreciated thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the lines aren't the same length, there's no way to start reading from the eighth line except for looking through the first part of the file and finding the first seven newlines, then starting to read. The easiest way to do this is probably just to read and discard the first seven lines using BufferedReader.readLine(), but if you insist, you could examine the individual characters, watching for \n's and \r's and counting them until you find where the eighth line starts.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option is to normalize the contents of that file, so that it looks like a properties file. Then you simply read the value off the keys without counting them or guessing where they are.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like that, John. Since all lines are name value pairs I'd probably read them all into a map and then just pull out the keys I needed. If you had to do a few million of them, the overhead of mapping some keys you don't really use might add up, but for reasonable data sets I'd make the code easy to read and maintain first.
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like a golden opportunity to try out XML with Java, if you are at liberty to redefine how the data file looks!
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
I like that, John. Since all lines are name value pairs I'd probably read them all into a map and then just pull out the keys I needed. If you had to do a few million of them, the overhead of mapping some keys you don't really use might add up, but for reasonable data sets I'd make the code easy to read and maintain first.



No need to load all the key-values into a map. You can use java.util.Properties and can get particular value by supplying its key.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd have to try to be sure ... will a properties file work with those spaces in the key names? If so, it's a better choice to use code that's already there for sure.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, I missed that.
I tried this, and got very surprising results:



It means, untill it finds '=' or 'first space' it consider everything is key and remaining is value

Hope I got the right concept.
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic