0

I'm trying to read a file from a .txt, the file is a few numbers. But when I run the program I'm getting input string is not in the correct format. I passed everything into integers using parse. What am I missing?

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            const int Size = 10; 
            int[] numbers = new int[Size];

            int index = 0; 

            StreamReader inputFile; 

            inputFile = File.OpenText("Sales.txt");

            while(index < numbers.Length && !inputFile.EndOfStream)
            {
                numbers[index] = int.Parse(inputFile.ReadLine());
                index++;
            }
            inputFile.Close();

            foreach( int value in numbers)
            {
                listBox1.Items.Add(value);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

}

2
  • 2
    Can you show us the sample data inside of inputFile? Commented Apr 4, 2016 at 23:05
  • 1245.67 1189.55 1098.72 1456.88 2109.34 1987.55 1872.36 Commented Apr 4, 2016 at 23:18

1 Answer 1

1

This is due to your input file consisting of doubles, not ints. Any non-whole number must be stored as a double or a float if you do not want to lose the numbers after the decimal point. Try the following code.

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        const int Size = 10; 
        double[] numbers = new double[Size];

        int index = 0; 

        StreamReader inputFile; 

        inputFile = File.OpenText("Sales.txt");

        while(index < numbers.Length && !inputFile.EndOfStream)
        {
            numbers[index] = double.Parse(inputFile.ReadLine());
            index++;
        }
        inputFile.Close();

        foreach(double value in numbers)
        {
            listBox1.Items.Add(value);
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

However, the issue may also be that your data isn't separated by lines, but by spaces. According to your comment, this may be the case. To fix that, you would use the following code.

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        foreach (double value in File.ReadAllText("Sales.txt").Split(' ').Select((x) => (double.Parse(x))))
        {
            listBox1.Items.Add(value);
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

What this solution does is ReadAllText from the file, which automatically handles streams, opening, and closing of the file. I pass that into the Split function, which splits a single String into substrings where the parameter is found. So, if you have 123.613 7342.152 as your String, it will return you a String[] that consists of 123.613 and 7342.152. However, these values are still Strings. We still need to parse them into numbers. We can do this by using the LINQ Select operator, and passing it in a lambda that will call double.Parse(...) on the value and return it as an IEnumerable<double>.

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

2 Comments

I switched it over to double instead of integer, and it worked out. Thanks!
No problem, glad to help!

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.