hi i have to group the data.
i completed coding.
the output is
[ output]

C:\Program Files\Java\jdk1.6.0_03\bin>java check

Please Enter the Number of Clusters
3
Enter the file to be read with csv extension
hi.csv

[(345,2) belongs to 0, (300,2) belongs to 0, (390,2) belongs to 0, (400,3) belongs to 0, (457,3) belongs to 2, (478,3) belongs to 2, (200,1) belongs to 1
, (234,1) belongs to 1, (280,1) belongs to 1]

[/output]
every thing works fine
but i have to print the data in excel sheet of form
group 0 group1 group2
(345,2) (234,1) (400,3)
(300,2) (200,1) (457,3)

how to do this in java.
can you help me with a method to do this.
thank you

You can print out the output in CSV (comma-separated-values) format and then open this file in a spreadsheet application like Excel. If this is not what you want, detail out your problem.

=java
public class Point 
 { 
  
    private int x; 
  
    private int y; 
  
    private int Number; 
  
  
    public Point(int X, int Y) 
        { 
  
        this.x =X; 
        this.y = Y; 
        this.Number=0; 
    }  
  
  
  
    public void assign(int clustNo)  
          { 
  
        this.Number = clustNo; 
  
    }  
  
  
  
    public int getNumber()  
        { 
  
        return this.Number; 
  
    }  
  
  
    public int getX()  
         { 
  
        return this.x; 
  
    }  
  
  
  
    public int getY()  
        { 
  
        return this.y; 
  
    }  
  
  
    public static double distance(Point dp1, Point dp2)  
        { 
  
        double result = 0; 
        double resultX = dp1.getX() - dp2.getX(); 
        double resultY = dp1.getY() - dp2.getY(); 
        result = Math.sqrt(resultX*resultX + resultY*resultY); 
        return result; 
  
    }  
  
  
  
    public  String toString() 
          {    
              return "("+ this.x + " ," +this.y + ")" + " belongs to g" + this.Number ;  
  
  
          } 
  
  
  
}  
  
import java.io.*; 
import java.util.*; 
  
  
  
public class trial 
 { 
  
  
        private int k; 
  
  
    private grouping[] group; 
  
  
  
    private int nIterations; 
  
  
  
    private Vector inputdata; 
  
  
  
    private String inputFileName; 
  
  
  
  
         public trial (int k, String inputFileName)  
         { 
  
        this.k = k; 
        this.inputFileName = inputFileName; 
        this.group= new grouping[this.k]; 
        this.nIterations = 0; 
        this.inputdata = new Vector(); 
  
      }  
  
  
  
  
           public trial(int k, List inputdata) 
  
           { 
  
        this.k = k; 
        this.inputFileName = inputFileName; 
        this.group= new grouping[this.k]; 
        this.nIterations = 0; 
        this.inputdata=new Vector(inputdata); 
  
        }  
  
  
  
  
  
       public void readData() throws IOException 
  
               { 
  
        BufferedReader in = new BufferedReader(new FileReader(this.inputFileName)); 
        String line = ""; 
           while ((line = in.readLine()) != null ) 
                      { 
  
            StringTokenizer st = new StringTokenizer(line, " \t\n\r\f,"); 
                if (st.countTokens() == 2)  
                                  { 
  
                    Point dp = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())); 
                    dp.assign(0); 
                    this.inputdata.add(dp); 
  
                                   } 
  
                } 
  
  
  
  
        in.close(); 
  
    }  
  
  
         public void runs()  
               { 
  
           // Select k points as initial means 
             for (int i=0; i < k; i++) 
                    { 
  
            this.clusters[i] = new clustering(i); 
            this.clusters[i].setMean((Point)(this.inputdata.get((int)(Math.random() * this.inputdata.size())))); 
  
               } 
  
  
  
  
          do  
                   { 
  
            Iterator i = this.inputdata.iterator(); 
                while (i.hasNext()) 
  
                        this.assign((Point)(i.next())); 
  
            this.nIterations++; 
  
        }while (this.updateMeans()); 
  
  
              }  
  
  
  
  
    private void clusterassign(Point dp) 
           { 
  
        int currentCluster = dp.getClusterNumber(); 
  
        double minDistance = Point.distance(dp, this.clusters[currentCluster].getMean());; 
  
        for (int i=0; i <this.k; i++) 
            if (DataPoint.distance(dp, this.clusters[i].getMean()) < minDistance)  
                             { 
  
                minDistance = DataPoint.distance(dp, this.clusters[i].getMean()); 
                currentCluster = i; 
  
                   } 
  
        dp.clusterassign(currentCluster);     
  
    }  
  
  
  
  
        private boolean updateMeans()  
           { 
  
        boolean reply = false; 
  
        int[] x = new int[this.k]; 
        int[] y = new int[this.k]; 
        int[] size = new int[this.k]; 
        Point[] pastMeans = new Point[this.k]; 
  
        for (int i=0; i<this.k; i++) 
                  { 
  
            x[i] = 0; 
            y[i] = 0; 
            size[i] = 0; 
            pastMeans[i] = this.clusters[i].getMean(); 
  
         } 
  
        Iterator i = this.inputdata.iterator(); 
        while (i.hasNext())  
                { 
  
  
            Point dp = (Point)(i.next()); 
            int currentCluster = dp.getClusterNumber(); 
  
            x[currentCluster] += dp.getX(); 
            y[currentCluster] += dp.getY(); 
            size[currentCluster]++; 
  
        } 
  
        for (int j=0; j < this.k; j++ )  
            if(size[j] != 0) { 
  
                x[j] /= size[j]; 
                y[j] /= size[j]; 
                Point temp = new Point(x[j], y[j]); 
                temp.clusterassign(j); 
                this.clusters[j].setMean(temp); 
                if (Point.distance(pastMeans[j], this.clusters[j].getMean()) !=0 ) 
                    reply = true; 
  
            } 
  
        return reply; 
  
    }  
  
    public int getK() { 
  
        return this.k; 
  
    }  
  
  
    public clustering getCluster(int index) { 
  
  
  
        return this.clusters[index]; 
  
    }  
  
  
  
    public String toString() 
           {         
  
                    return  this.inputdata.toString(); 
  
  
  
  
    }  
  
  
  
    public Vector getPoints() { 
  
        return this.inputdata ; 
  
    }  
  
  
  
  
  
  
    public static void main(String[] args) { 
  
  
         System.out.println("Please Enter the Number "); 
         Scanner input1 = new Scanner(System.in); 
         int n=input1.nextInt(); 
         System.out.println(" Enter the file name"); 
         Scanner input2 = new Scanner(System.in); 
         String name = input2.next(); 
         File file = new File(name); 
  
        trial  km = new  trial(n, name); 
  
        try { 
            km.readData(); 
        } catch (Exception e) { 
            System.err.println(e); 
            System.exit(-1); 
        } 
  
  
        km.runs(); 
  
  
                 System.out.println(" \n" +km); 
  
  
        }  
  
}

[output]
C:\Program Files\Java\jdk1.6.0_03\bin>java check

Please Enter the Number of Clusters
3
Enter the file to be read with csv extension
hi.csv

[(345,2) belongs to 0, (300,2) belongs to 0, (390,2) belongs to 0, (400,3) belongs to 0, (457,3) belongs to 2, (478,3) belongs to 2, (200,1) belongs to 1
, (234,1) belongs to 1, (280,1) belongs to 1]

[/output]
every thing works fine
but i have to print the data in excel sheet of form
group 0 group1 group2
(345,2) (234,1) (400,3)
(300,2) (200,1) (457,3)

What i am getting as output is what i am printing in my toString. i have to add these points to an ArrayList of ArrayLists. Those in group0 go into the ArrayList at position 0 in the arraylist, those in group one go into the arraylist at position 1 e.t.c.
i could then write a print method for that takes that arraylist and prints out its contents the way i want.
Please help me to do this. since i am naive to java
i don't understand the concept ArrayofArray List. Please if you can do this it will be of great help. please help in coding. It is urgent and i have to finish my project.

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.