3

these are the values from the textfile

1245.67
1189.55
1098.72
1456.88
2109.34
1987.55
1872.36

they are obviously decimals

not sure what i'm missing but when i debug i get Input string was not in a correct format any help would be great

thats what i coded so far

    private void getValuesButton_Click(object sender, EventArgs e)
    {
        try
        {
            //create an array to hold items read from the file.
            const int SIZE = 7;
            double[] numbers = (double[])ois.readObject();

            // Counter variable to use in the loop
            int index = 0;

            //Declare a StreamReader variable 
            System.IO.StreamReader inputFile;

            //Open the file and get a StreamReader object.
            inputFile = File.OpenText("Values.txt");

            //Read the file contents into the array.
            while (index < numbers.Length && !inputFile.EndOfStream)
            {
                numbers[index] = int.Parse(inputFile.ReadLine());
                index++;
            }

            //Close the file.
            inputFile.Close();

            //Display the array elements in the list box.
            foreach (double value in numbers)
            {
                outputListbox.Items.Add(value);
            }
        }
        catch (Exception ex)
        {
            //Display an error message.
            MessageBox.Show(ex.Message);
        }
Ahmad Azwar Anas
  • 1,289
  • 13
  • 22
user2451489
  • 31
  • 1
  • 4

2 Answers2

1

If the file is UTF8 format and contains nothing but floating point numbers one per line, you can parse them all into a sequence like so (in the current locale)

var fileNumbers = File.ReadLines(filename).Select(double.Parse);

This works because File.ReadLines() returns an IEnumerable<string> which returns each string from the file in sequence. Then I use Linq's Select() to apply double.Parse() to each line, which effectively converts the sequence of strings to a sequence of doubles.

Then you can use the sequence like this:

int index = 0;

foreach (var number in fileNumbers)
    numbers[index++] = number;

Or you can omit the intermediary numbers array and put them straight into the list box:

foreach (var number in fileNumbers)
    outputListbox.Items.Add(number);

You could do the whole thing in two lines, but this is much less readable:

foreach (var number in File.ReadLines("filename").Select(double.Parse))
    outputListbox.Items.Add(number);

Finally, as noted by Ilya Ivanov below, if you only need the strings in the listbox you can simply do this:

outputListbox.Items.AddRange(File.ReadAllLines(filename));
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • if you are talking about `UTF8`, shouldn't you specify it, like `File.ReadLines(filename, Encoding.UTF8)` ? – Ilya Ivanov Jun 04 '13 at 11:39
  • im currently confused big time the file type is .txt – user2451489 Jun 04 '13 at 11:40
  • @user2451489 Yes but `double.Parse()` converts from text to double. – Matthew Watson Jun 04 '13 at 11:44
  • @IlyaIvanov As the documentation for `File.ReadLines()` says: `"This method uses UTF8 for the encoding value."` – Matthew Watson Jun 04 '13 at 11:46
  • @MatthewWatson well, actually documentations states following `method attempts to automatically detect the encoding of a file based on the presence of byte order marks. Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.` [source](http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx) – Ilya Ivanov Jun 04 '13 at 11:47
  • @IlyaIvanov I'm looking at [this documentation](http://msdn.microsoft.com/en-us/library/dd383503.aspx). It's unlikely that a text file containing a list of numbers will have a BOM of course, but if it did it would work either way. Note that I'm using `File.ReadLines()` and you're using `File.ReadAllLines()`. – Matthew Watson Jun 04 '13 at 11:51
  • My fault, confused `ReadAllLines` with `ReadLines`. By the way, note that there is no need to parse objects. You can use `AddRange` and pass a `object[]` where each instance will be a plain string from file. – Ilya Ivanov Jun 04 '13 at 11:53
  • @IlyaIvanov Yes good point - parsing to doubles will however ensure that only valid doubles can be used (else an exception will occur). This would also be important if the ListBox items are being cast to doubles somewhere. – Matthew Watson Jun 04 '13 at 11:55
0

Looks like you did int.parse, you are trying to parse a double though, not an int, so use Double.Parse where you are reading into the array and this should work fine.

bigcakes
  • 216
  • 2
  • 9