sam
sam

Reputation: 15

reading from a file, input string is incorrect C#

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);
        }
    }
}

}

Upvotes: 0

Views: 180

Answers (1)

Patrick Bell
Patrick Bell

Reputation: 769

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>.

Upvotes: 1

Related Questions