944,188 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5612
  • Java RSS
Oct 10th, 2006
-1

Parsing CSV file in partcular format

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Zulfiqarmd is offline Offline
2 posts
since Oct 2006
Oct 11th, 2006
0

Re: Parsing CSV file in partcular format

Use some kind of <map> that takes care of repeats?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 12th, 2006
0

Re: Parsing CSV file in partcular format

[HTML]
Java Syntax (Toggle Plain Text)
  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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Zulfiqarmd is offline Offline
2 posts
since Oct 2006
Oct 12th, 2006
0

Re: Parsing CSV file in partcular format

>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:-

Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: difference java javascript
Next Thread in Java Forum Timeline: how to put a midlet on device (J2ME)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC