4 Replies - 2924 Views - Last Post: 02 January 2012 - 06:10 AM Rate Topic: -----

#1 shankar14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-January 12

reading value from file and storing it in 2D Array

Posted 02 January 2012 - 12:51 AM

i have to read values stored in a file, and to store it in double dimensional array
for example i took file contain an values
123
321
231
132
like so many sequence values, i have to read these values and store it in double dimensional array and finally display that array content how can i do that please help me to sort this problem
this is code till now i able to develop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication12
{
    class Program
    {
       
             static void Main()
        {
     
          String line;
            try
            {
                double[,] sh=new double[3,1];
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader(@"D:\input.txt");

                //Read the first line of text
                line = sr.ReadLine();

                //Continue to read until you reach end of file
                while (line != null)
                {
                    //write the line to console window

                    sh = Double.TryParse(line);
                      

                  //  Console.WriteLine(line);

                   line = sr.ReadLine();
                }

                //close the file
                sr.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }

but i am getting error, while assigning input value to double dimensional array

This post has been edited by CodingSup3rnatur@l-360: 02 January 2012 - 03:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: reading value from file and storing it in 2D Array

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: reading value from file and storing it in 2D Array

Posted 02 January 2012 - 02:01 AM

please use the code tags
:code:

also what error do you get? also why do you need 2D array when your input is just one dimension? and if you want to input the value you should point at which element to input it. what i mean is like this:
sh[0]=123.123
sh[1]=234.234
sh[2]=345.345
...


This post has been edited by raziel_: 02 January 2012 - 02:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 shankar14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-January 12

Re: reading value from file and storing it in 2D Array

Posted 02 January 2012 - 02:40 AM

View Postraziel_, on 02 January 2012 - 02:01 AM, said:

please use the code tags
:code:

also what error do you get? also why do you need 2D array when your input is just one dimension? and if you want to input the value you should point at which element to input it. what i mean is like this:
sh[0]=123.123
sh[1]=234.234
sh[2]=345.345
...


actually i am passing this value to k means alogorithm for clustering it oly takes double value
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: reading value from file and storing it in 2D Array

Posted 02 January 2012 - 03:37 AM

another thing TryParse takes 2 arguments:
http://msdn.microsof...e.tryparse.aspx
you need to tell at what element from your array you need to pass the values from your file.

This post has been edited by raziel_: 02 January 2012 - 03:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: reading value from file and storing it in 2D Array

Posted 02 January 2012 - 06:10 AM

Several things to note:

1. Format your code before posting: it's quick and easy (if you're using VS just click Ctrl+K,D)
2. Use using statements when you're dealing with streams, that way you don't have to worry about flushing or closing your stream:

using (var sr = new StreamReader(@"D:\input.txt"))
{
//Code here...
}


3. You have some strange logic: first you read the line out of the loop, then you parse it IN the loop and read it again in the loop... WTF?! :D

You should do:

1. Declare a counter
2. While the readline method doesn't return nothing:
3. Declare a local temporary value to hold your numbers
4. parse the line read into a double
5. Add it into the array *
6. Increase the counter

Spoiler


*I added it in this manor, you may want to do it differently in your case...

NOTE: You could also check if the method parsed the number successfully: the tryParse method returns a bool indicating if it has been able to parse the number...

Spoiler


You may want to read these articles:
Multidimensional Arrays (C# Programming Guide)
Double.TryParse Method

This post has been edited by RexGrammer: 02 January 2012 - 06:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1