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
Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
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.
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"
Skip 12 lines above it.
Is there anything like that?
Joined: Oct 13, 2005
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 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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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 )
[Edit] Actually, Point is a bad choice. It's inherently unsafe. But a wrapper to a Point...
Isn't it funny how there's always time and money enough to do it WRONG?