Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReadFromFile
{
   public class k_means1
    {
     static void Main(string[] args)
      {
       Console.WriteLine("\nBegin k-means clustering demo\n");
       StreamReader reader = new StreamReader(@"F:\DOT NET PROJECTS\2012 projects\scalable learning of collective behaviour\app.txt");
          string content = null;
          string[] line = null;

          char[] ch = { ',' };

          int a = 1;
          double[][] rawdata = new double[20][];
          while ((content = reader.ReadLine()) != null)
          {
              if (a == 1 || a == 2)
                  Console.WriteLine("Does nothing");
              else
              {
                  line = content.Split(ch);
                 // try
                  //{
                      for (int b = 0; b < 20; b++)
                      {
                          rawdata[b] = new double[2];
                          rawdata[b][0] = Convert.ToDouble(line[1]);
                          rawdata[b][1] = Convert.ToDouble(line[2]);
                      }

                  //}
                  //catch (Exception ex)
                  //{
                    //  Console.WriteLine(ex.Message);
                  //}
                  //Console.WriteLine("First value: " + line[1]);
                  //Console.WriteLine();
                  //Console.WriteLine("First value: " + line[2]);
              }
              a++;
           }
          foreach (double[] d in rawdata)
          {
              Console.WriteLine("First value: " + line[1]);
              Console.WriteLine();
              Console.WriteLine("First value: " + line[2]);
          }
       Console.WriteLine("Raw unclustered data:\n");
       Console.WriteLine("    Height Weight");
       Console.WriteLine("-------------------");
       ShowData(rawdata, 1, true, true);

      int numClusters = 3;
      Console.WriteLine("\nSetting numClusters to " + numClusters);

      int[] clustering = Cluster(rawdata, numClusters); // this is it

      Console.WriteLine("\nK-means clustering complete\n");

      Console.WriteLine("Final clustering in internal form:\n");
      ShowVector(clustering, true);

      Console.WriteLine("Raw data by cluster:\n");
      ShowClustered(rawdata, clustering, numClusters, 1);

      Console.WriteLine("\nEnd k-means clustering demo\n");
      Console.ReadLine();
          }


error will be there in for loop statement that statement is like as


rawdata[b][0] = Convert.ToDouble(line[1]);

input string is not correct format.


pls help me.
Posted
Comments
Kornfeld Eliyahu Peter 21-May-14 6:02am    
Debug it and see what line[1] contains! It's something that can not be converted to double...
CPallini 21-May-14 6:05am    
What's the 'app.txt' file content? Could you please post here a little fragment of it?

1 solution

Safer way to convert to double.

double value;
if(double.TryParse(line[1], out value)){
rawdata[b][0] = value;
}


But the actual solution is to debug and figure out which value is creating the error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900