This content has been marked as final. Show 46 replies
Thanks for your great response.
It was really helpful for me.
My problem was quite different, I could not explain it properly.
Here is the actual problem.
My input file will be like this:-
I need output in this manner:-
Input.csv LEVEL,1,1,23,DMY,200,198,2 LEVEL,1,1,23,4AS,658,657,1 LEVEL,1,1,23,5ON,214,212,2 LEVEL,1,2,24,DMY,1232,1230,2 LEVEL,1,2,24,4AS,735,730,5 LEVEL,1,2,24,5ON,650,645,5 LEVEL,1,1,25,DMY,200,198,2 LEVEL,1,1,25,4AS,658,657,1 LEVEL,1,1,25,5ON,214,212,2 LEVEL,1,2,26,DMY,1232,1230,2 LEVEL,1,2,26,4AS,735,730,5 LEVEL,1,2,26,5ON,650,645,5 LEVEL,1,1,27,DMY,200,198,2 LEVEL,1,1,27,4AS,658,657,1 LEVEL,1,1,27,5ON,214,212,2 LEVEL,1,2,28,DMY,1232,1230,2 LEVEL,1,2,28,4AS,735,730,5 LEVEL,1,2,28,5ON,650,645,5 LEVEL,1,1,29,DMY,200,198,2 LEVEL,1,1,29,4AS,658,657,1 LEVEL,1,1,29,5ON,214,212,2 LEVEL,1,2,30,DMY,1232,1230,2 LEVEL,1,2,30,4AS,735,730,5 LEVEL,1,2,30,5ON,650,645,5 ELASTICITY,1,1,23,DMY,200,198,2 ELASTICITY,1,1,23,4AS,658,657,1 ELASTICITY,1,1,23,5ON,214,212,2 ELASTICITY,1,2,24,DMY,1232,1230,2 ELASTICITY,1,2,24,4AS,735,730,5 ELASTICITY,1,2,24,5ON,650,645,5 ELASTICITY,1,1,25,DMY,200,198,2 ELASTICITY,1,1,25,4AS,658,657,1 ELASTICITY,1,1,25,5ON,214,212,2 ELASTICITY,1,2,26,DMY,1232,1230,2 ELASTICITY,1,2,26,4AS,735,730,5 ELASTICITY,1,2,26,5ON,650,645,5 ELASTICITY,1,1,27,DMY,200,198,2 ELASTICITY,1,1,27,4AS,658,657,1 ELASTICITY,1,1,27,5ON,214,212,2 ELASTICITY,1,2,28,DMY,1232,1230,2 ELASTICITY,1,2,28,4AS,735,730,5 ELASTICITY,1,2,28,5ON,650,645,5 ELASTICITY,1,1,29,DMY,200,198,2 ELASTICITY,1,1,29,4AS,658,657,1 ELASTICITY,1,1,29,5ON,214,212,2 ELASTICITY,1,2,30,DMY,1232,1230,2 ELASTICITY,1,2,30,4AS,735,730,5 ELASTICITY,1,2,30,5ON,650,645,5
In above output file, first four columns are key for each row. First it checks for records containing same key, if it finds then it will put 5th value from each row, then 6th value and then 7th value from each row. for example for first three records from input.csv file key is LEVEL,1,1,23 and it will read 5th value from each row i.e. 200,658,214 then 6th value from each row i.e. 198, 657, 212, and then 7th value from each row i.e. 2 , 1 , 2, .
output.csv LEVEL, 1,1,23,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , LEVEL, 1,2,24,1232,735, 650, 1230,730, 645, 2 , 5 , 5 , LEVEL, 1,1,25,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , LEVEL, 1,2,26,1232,735, 650, 1230,730, 645, 2 , 5 , 5 , LEVEL, 1,1,27,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , LEVEL, 1,2,28,1232,735, 650, 1230,730, 645, 2 , 5 , 5 , LEVEL, 1,1,29,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , LEVEL, 1,2,30,1232,735, 650, 1230,730, 645, 2 , 5 , 5 , ELASTICITY,1,1,23,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , ELASTICITY,1,2,24,1232,735, 650, 1230,730, 645, 2 , 5 , 5 , ELASTICITY,1,1,25,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , ELASTICITY,1,2,26,1232,735, 650, 1230,730, 645, 2 , 5 , 5 , ELASTICITY,1,1,27,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , ELASTICITY,1,2,28,1232,735, 650, 1230,730, 645, 2 , 5 , 5 , ELASTICITY,1,1,29,200, 658, 214, 198, 657, 212, 2 , 1 , 2 , ELASTICITY,1,2,30,1232,735, 650, 1230,730, 645, 2 , 5 , 5 ,
The same procedure is followed for each line.
Please respond with sample code.
Assuming that I understood your algorithm correctly, here is a probably inefficient but transparent algorithm
1.) Read all lines into an ArrayList<String>.
2.) Read the ArrayList extracting keys only and add the keys to a Set<String>. In this step all you do is read the first 3 values and add them to the Set<String>
3.) Read the ArrayList values again this time extracting the values to be appended to your Set<String> values.
I am able to get unique key set. Using unique key set not able to combine the multiple values from different row's, which has the same key.
Let's take a input.csv file example:-
for the above code key will be LEVEL,1,1,23
LEVEL,1,1,23,DMY,200,198,2 LEVEL,1,1,23,4AS,658,657,1 LEVEL,1,1,23,5ON,214,212,2
Now I have to marge all three rows because all has the same key.
So the output should be like this:-
200 is from first row, 658 from second row and 214 from third row. this repeats for other values also.
This is what I wants to achieve
You have to read the ArrayList again and then locate the key in the Set for which the record belongs to. You then extract the value you want (say fifth) and append it to the key. You then run through the ArrayList again this time adding the sixth values to the key and again for the seventh values. That is why I said that it would not be efficient.
It can be improved but it's probably best that you get the correct output first.
Please respond with sample code.Did you actually read any of the reponses you've already got? And you can't think how to adapt them to your requirements?
Jeezus! I did all that work for just another "sned me teh codez" a-hole.
McDonalds are still hiring, I think. Good luck with the interview.
JavaMani wrote:That is simply not going to happen. You have been given pointers and usable code already. You are going to have to start implementing the algorithm yourself and post if you get problems with your code.
Can any one send same sample code to implement the functionality posted in previous messages?
You have more than enough of samples of how the logic looks when developing the algorithm to do what you need, if we do it for you, you might be as lucky next time you run into a problem like this one. First write out the spec.'s you'd expect the program would have to follow to meet your needs and then check it and then try to implement it. Ask us when you have a problem with a specific implementation of your spec.'s.
If you can't figure out how to turn an ArrayList into a set then go tell you're boss that you're very sorry for wasting his time, but you simply can't do the job he's hired you to do, because you're simply not capable of doing it. Then offer him a full refund of any payment he has given you to date. I amost guarantee you that your boss will appreciate your honesty; and far from being angry s/he may even attempt to find another place for you... raw intelligence is not the only attribute that's worth hiring, it's just the one that happens to be the chief prerequisite for computer programming... and for every programmer there's three or four "support staff"... maybe s/he needs a good tester... testers need to be honest.
Pretending to do a job which is beyond you is dishonest. Harsh but true.