Reputation: 4669
Question: I have two files one with list of serial number,items,price, location and other file has items. So i would like compare two files and printout the number times items are repeated in file1 with serial number.
Text1 file will have
Text2 file will have
Output should be
So the file1 is not formatted in proper order and file 2 is in order (line by line).
Upvotes: 0
Views: 1594
Reputation: 11226
Use regex.
Step one, tracing and splitting at [\d,], store results in map Step two, read in the word from the second file. say it's "pen" Step three, do regex search "pen" on each string within the map. Step four, if the above returns true , do something like ([A-Z][A-Z],) on each string within the map.
Upvotes: 0
Reputation: 1596
Such problems are not best solved by monolithic JAVA code. If you don't have tool constraint then recommended way to solve it is to import data from file 1 into a database table and then run queries from your program to fetch whatever information you like. You can easily select serial numbers based on items and group them for count based on location.
This approach will ensure that you can keep up with changing requirements and if your files are huge you will have good performance.
I hope you are well versed with SQL and DB tools, so I have not posted any details on them.
Upvotes: 0
Reputation: 51030
Even though your file1 is not well formatted, it's content has some pattern which you can use to read it successfully.
For each item, it has all the information (i.e. serial number, name, price, location) but not in a certain order. So, you have pay attention to and use the following patterns while you read each item from the file1 -
$
and .
character.Upvotes: 0
Reputation: 3054
Okay my approach to this would be
Upvotes: 0
Reputation: 6259
Since you have no apparent code or effort put into this, I'll only hint/guide you to some tools you can use.
For parsing strings: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
For reading in from a file: http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml
And I would recommend reading file #2 first and saving those values to an arraylist, perhaps, so you can iterate through them later on when you do your searching.
Upvotes: 3