d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Most Efficient Way To Search For Line In Text File
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
May 23 2013 06:30pm
I have a small program (written in java) which I was looking to add some little utility to which might be more of a headache than I thought.

So essentially it is just a small GUI program which I use to keep track of bills I have paid (along w/ authentication #'s, since I pay online mostly)

The program stores the company name, date, amount paid, authorization # into a text file.

However, I would like it if the program could show me the last time I paid a certain bill, the best way to accomplish this it would seem is to use more file i/o.

So one approach I thought of was using a scanner to parse the file line by line and then store each line into a string and maybe use a pattern/matcher to find a particular regex (Each line is of the structure: company_name, date, amount_paid, auth_number)

However, this seems like a bad approach because the last bill payment would obviously be at the end of the file (so I would be parsing the entire file until I found the last instance of something probably)

Another option I was thinking would be a separate file but that also kinda seems inefficient. (To just store the date and possibly an identifier tag)

I am not terribly familiar with solving this kind of problem, was wondering if anyone could tip me to a better mindset of solving the problem.

This post was edited by Eep on May 23 2013 06:30pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 23 2013 06:39pm
well loading the entire file does not seem like a serious problem to me since what is realistically the max number of possible lines? 1000? that is not going to be a problem. If you had a few million lines (paid bills)... well you would have problems (financial and programming).

just load each line and split it in to an array. Then load the arrays into an array.

This post was edited by Azrad on May 23 2013 06:41pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 23 2013 06:59pm
performance wise will be fine. personally, i'd consider putting it into a relational DB or an excel sheet. if it's in CSV format it should be fine. that way you can do more complex analysis on it.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 23 2013 07:15pm
Duck's way. He also says to just export everything to mysql which is not difficult at all.

Code

bills = []
fd = File.open('mybills.txt').read
fd.gsub!(/\r\n?/. "\n")
fd.each_line { |line| bills.push line.split /[ ]/ }
bills.each { |item| puts item }


Code

bills = []
=> []

line = "visa #0128121 $122.22"
=> "visa #0128121 $122.22"

bills.push line.split /[ ]/
=> [["visa", "#0128121", "$122.22"]]

line = "carloan #836432 $1122.22"
=> "carloan #836432 $1122.22"

bills.push line.split /[ ]/
=> [["visa", "#0128121", "$122.22"], ["carloan", "#836432", "$1122.22"]]

bills.each { |bill| puts bill }
visa
#0128121
$122.22
carloan
#836432
$1122.22
=> [["visa", "#0128121", "$122.22"], ["carloan", "#836432", "$1122.22"]]
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
May 23 2013 08:26pm
can't believe I never thought of doing that. Huh.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll