Mad-D
Mad-D

Reputation: 4669

Read two files (text) and compare for common values and output the string?

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

enter image description here

Text2 file will have

enter image description here

Output should be

enter image description here

So the file1 is not formatted in proper order and file 2 is in order (line by line).

Upvotes: 0

Views: 1594

Answers (5)

John Eipe
John Eipe

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

Bipul
Bipul

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

Bhesh Gurung
Bhesh Gurung

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 -

  • Serial number is always a plain integer.
  • Price has that $ and . character.
  • Location is 2-character long, all capital.
  • And name is a string can not be any of the above.

Upvotes: 0

S.P.
S.P.

Reputation: 3054

Okay my approach to this would be

  1. Read in the file1 and file2 into a string
  2. "Split" the string in file 1 as well as file2 based on "," if that is what is being used
  3. Check for the item in every 3rd one so my iteration would iterate +3 every time (You might need to sort if not in order both of these)
  4. If found store in an Array,ArrayList etc. Go back to Step 3 if more items present. Else stop

Upvotes: 0

Max
Max

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

Related Questions