Parsing CSV file in partcular format

Reply

Join Date: Oct 2006
Posts: 2
Reputation: Zulfiqarmd is an unknown quantity at this point 
Solved Threads: 0
Zulfiqarmd Zulfiqarmd is offline Offline
Newbie Poster

Parsing CSV file in partcular format

 
0
  #1
Oct 10th, 2006
Hi all,
I am working on generating CSV month by month for annual report. what I do is just create CSV file initially and append to it every month end. So as to generate annual CSV file in following format:

Month 1, Nokia8800, 156.0
Month 1, Nokia3120, 113.0
Month 2, Nokia8800, 16.0
Month 2, Nokia3120, 152.0
Month 3, Nokia8800, 44.0
Month 3, Nokia3120, 52.0 and so on for 12 months.

Now what i need is to re-format this CSV in follwoing file format:
Month 1,2,3,4,5,6,7,8,9,10,11,12
Nokia8800,156.0,16.0,44.0
Nokia3120, 113.0,152.0,52.0 and so on for 12 months.

How can i get this done? any logic or code welcome?

thanks in advance.
Zulfiqar
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Parsing CSV file in partcular format

 
0
  #2
Oct 11th, 2006
Use some kind of <map> that takes care of repeats?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2
Reputation: Zulfiqarmd is an unknown quantity at this point 
Solved Threads: 0
Zulfiqarmd Zulfiqarmd is offline Offline
Newbie Poster

Re: Parsing CSV file in partcular format

 
0
  #3
Oct 12th, 2006
[HTML]
  1. import java.io.File;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.util.StringTokenizer;
  5.  
  6. public class readCSV {
  7. public static void main(String args[]) {
  8. try{
  9. File file = new File("c:/java/Number of Requests per Device.csv");
  10. BufferedReader bufRdr = new BufferedReader(new FileReader(file));
  11.  
  12. String line = null;
  13. int monthNumber = 0;
  14. int deviceName = 0;
  15. int activeUsers = 0;
  16.  
  17. String vals[][][] = new String[100][100][100];
  18.  
  19. //read each line of text file
  20. while((line = bufRdr.readLine()) != null)
  21. {
  22. StringTokenizer st = new StringTokenizer(line,",");
  23.  
  24. while (st.hasMoreTokens())
  25. {
  26. //get next token and store it in the array
  27. vals[monthNumber][deviceName][activeUsers] = st.nextToken();
  28. System.out.println(vals[monthNumber][deviceName][activeUsers]);
  29. }
  30.  
  31. /*
  32.   deviceName=0;
  33.   activeUsers=0;
  34.   monthNumber++;
  35.   System.out.println(vals[monthNumber][0][0]);
  36.   */
  37. }
  38. //System.out.println(vals[monthNumber][0][0]);
  39. }catch(Exception e){
  40. System.out.println("Exception Fired is:" + e);
  41. }
  42. }
  43. }
[/HTML]

CSV file sample is as follows:
Month 1,blackberry8700,6397.0
Month 1,blackberry7730,1456.0
Month 1,blackberry7290,1113.0
Month 1,blackberry7100,727.0
Month 2,blackberry8700,1327.0
Month 2,blackberry7730,316.0
Month 2,blackberry7290,16.0
Month 2,blackberry7100,27.0

How do i iterate through all this elements and store in and array or some suitable data structure like unique month numbers in [months], device names in [device name] and active users in [active users] and retreive it back and store it in a following fashion:

Month 1,2
blackberry8700,6397.0,1327.0
blackberry7730,1456.0,316.0
blackberry7290,1113.0,16.0
blackberry7100,727.0,27.0

can some one help its urgent and im stuck!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Parsing CSV file in partcular format

 
0
  #4
Oct 12th, 2006
>can some one help its urgent and im stuck!!

I'm sure all homework is urgent. Tee he he. Anyway have you looked into my suggestion?

Like I said the first thing, well maybe not the first thing, but at some stage you'll have to find a way of eliminating repeats from the second colum in your list.

The best way is to do something like so:-

  1. import java.util.*;
  2.  
  3. public class set1 {
  4. public static void main(String args[])
  5. {
  6. Set hs = new HashSet();
  7. hs.add("1");
  8. hs.add("2");
  9. hs.add("2");
  10. hs.add("3");
  11.  
  12. Iterator iter = hs.iterator();
  13. while (iter.hasNext())
  14. System.out.println(iter.next());
  15. }
  16. }
with output of:

3
2
1


Notice there ain't no repeats! You'll need that.

The next question I ask is why are you creating a 3D array of strings?
Yeah. Nope that's asking for trouble. Instead create a class.
  1. class Phone
  2. {
  3. string blah;
  4. string blah2;
  5. string blah3;
  6. }

Or create three single one dimensional string arrays. Which I think is what you were prolly trying to do anyway.

There you go kiddo.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC