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

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

Use some kind of <map> that takes care of repeats?

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

public class readCSV {
    public static void main(String args[]) {
        try{
            File file = new File("c:/java/Number of Requests per Device.csv");
            BufferedReader bufRdr  = new BufferedReader(new FileReader(file));

            String line = null;
            int monthNumber = 0;
            int deviceName = 0;
            int activeUsers = 0;

            String vals[][][] = new String[100][100][100];

            //read each line of text file
            while((line = bufRdr.readLine()) != null)
            {    
                StringTokenizer st = new StringTokenizer(line,",");

                while (st.hasMoreTokens())
                {
                    //get next token and store it in the array
                    vals[monthNumber][deviceName][activeUsers] = st.nextToken();
                    System.out.println(vals[monthNumber][deviceName][activeUsers]);
                }

                /*
                deviceName=0;
                activeUsers=0;
                monthNumber++;
                System.out.println(vals[monthNumber][0][0]);
                */
            }
            //System.out.println(vals[monthNumber][0][0]);
        }catch(Exception e){
            System.out.println("Exception Fired is:" + e);
        }
    }
}

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

Member Avatar for iamthwee

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

import java.util.*;
  
  public class set1 {
    public static void main(String args[])
    {
      Set hs = new HashSet();
      hs.add("1");
      hs.add("2");
      hs.add("2");
      hs.add("3");
  
      Iterator iter = hs.iterator();
      while (iter.hasNext())
        System.out.println(iter.next());
    }
  }

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.

class Phone
{
    string blah;
    string blah2;
    string blah3;
}

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

There you go kiddo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.