• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Skip Reading several Lines using BufferedReader

 
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys I am reading a text file but I do not want to read the whole of it. Is there a way I can make the Buffered Reader ignore or skip reading the lines between 57 and 88 and also the lines between 129 and 160?

Here is the code I am using

 
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I don’t think you can. You can count the lines and simply ignore those with the inappropriate numbers.But hard-coding such numbers and ranges is very error-prone, leading to brittle code if there are any changes in the format of your file.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ritchie for that insight. The lines I want to Skip have similar content. I am wondering whether we can have a code for :
if line starts with "STATEMENT OF ACCOUNT"
then
Skip 12 lines above it.
Is there anything like that?
 
Campbell Ritchie
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do that easily if you had the lines in a List<String>. You can simply remove the last 12 lines before adding the next line. But be very careful that you really need to remove 12 lines, not 11 or 13 sometimes.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ritchie, am very new to Java so I would not know how to do that. A sample code please?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Ritchie, am very new to Java so I would not know how to do that. A sample code please?



Look at the documentation for java.util.List, and if you can't figure anything out, google for java collecitons tutorial.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Ritchie, am very new to Java so I would not know how to do that. A sample code please?


We don't provide code for you here. We feel it works best when you write the code and then come back with questions about why it does or doesn't work how you expect. So, give it a shot yourself, then come back and let us know where you get stuck.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you might want to try Campbells original suggestion first(even though i agree the style sucks) that can get it "working" then try making it better. i have compared to "starts with" before
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Hi guys I am reading a text file but I do not want to read the whole of it. Is there a way I can make the Buffered Reader ignore or skip reading the lines between 57 and 88 and also the lines between 129 and 160?


Java is an Object-Oriented language, so here's a thought: create a Class that does it for you.

I suspect mine might look something like this (I've only included the basics):and there's probably several ways to improve performance as well.
I leave the Range class up to you (me, I'd probably just extend java.awt.Point and add a contains() method; but I'm lazy )

HIH

Winston

[Edit] Actually, Point is a bad choice. It's inherently unsafe. But a wrapper to a Point...
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic