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);
            }
        }
5
  • 1
    have you gone through your loops with the debugger? Commented Aug 21, 2015 at 7: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. Commented Aug 21, 2015 at 8: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. Commented Aug 21, 2015 at 9:23
  • Side note: put System.IO.StreamReader into using, i.e. using(System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString())) {...} Commented Aug 21, 2015 at 9:43
  • @X-TECH text file format is .txt also every number is starting from new line. Commented Aug 21, 2015 at 9:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Problem Solved. It worked. Thank you Very much for your kind support.
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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.