4

I'm trying to read a text file and store it's data to an array list. It's working without any errors. Inside of my text file is like this.

277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23 

but in console output I can see this much of data.

277.18
311.13
349.23
349.23
**329.63
329.63
293.66
293.66
261.63
293.66
329.63
349.23
392**
277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23

Also Bolded numbers are not in my text file. Here is my code. how to solve this?? can Someone help me.. please...

        OpenFileDialog txtopen = new OpenFileDialog();
        if (txtopen.ShowDialog() == DialogResult.OK)
        {
            string FileName = txtopen.FileName;
            string line;
            System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString());
            while ((line = file.ReadLine()) != null)
            {
                list.Add(double.Parse(line));
            }
            //To print the arraylist
            foreach (double s in list)
            {
                Console.WriteLine(s);
            }
        }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
shona92
  • 63
  • 1
  • 12
  • 1
    have you gone through your loops with the debugger? – Sebastian L Aug 21 '15 at 07:56
  • Your variable FileName is already a string so you don't need to call ToString(). Also local variables should start with a lowercase letter. – Vincent Aug 21 '15 at 08:01
  • @shona92 Your code is correct but your text file format is doubtful. You need to set every in new line as you shown in your input. – NASSER Aug 21 '15 at 09:23
  • Side note: put `System.IO.StreamReader` into *using*, i.e. `using(System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString())) {...}` – Dmitry Bychenko Aug 21 '15 at 09:43
  • @X-TECH text file format is .txt also every number is starting from new line. – shona92 Aug 21 '15 at 09:47

2 Answers2

2

I think your list already contains some of the data, you should clear it before adding new file data to it.

OpenFileDialog txtopen = new OpenFileDialog();
if (txtopen.ShowDialog() == DialogResult.OK)
{
    list.Clear();   // <-- clear here

    string FileName = txtopen.FileName;
    string line;
    System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString());
    while ((line = file.ReadLine()) != null)
    {
        list.Add(double.Parse(line));
    }
    //To print the arraylist
    foreach (double s in list)
    {
        Console.WriteLine(s);
    }
}

Other wise every thing seems to be fine here.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
1

Try using Linq which doesn't add anything to existing list:

if (txtopen.ShowDialog() == DialogResult.OK) {
  var result = File
    .ReadLines(txtopen.FileName)
    .Select(item => Double.Parse(item, CultureInfo.InvariantCulture));

  // if you need List<Double> from read values:
  //   List<Double> data = result.ToList();
  // To append existing list:
  //   list.AddRange(result);

  Console.Write(String.Join(Environment.NewLine, result));
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215