Reading Lines From a File Into Two Double Precision Arrays

wellinth

New member
Joined
Oct 1, 2016
Messages
2
Programming Experience
Beginner
I just started learning C# and I am trying to read data from the text file below into two double precision arrays, xs and ys. When I run my code I get the error
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.
at the line xs[n] = double.Parse(newbits[0]);

What am I doing wrong?

- Tom
Here is the file, Forbes_One_Space.txt. From each line, I am trying the first number into the array xs and the second number into the array ys.
So, after reading the first line of the file, xs[0] should = 194.5 and ys[0] should = 131.79

194.5 131.79
194.3 131.79
197.9 135.02
198.4 135.55
199.4 136.46
199.9 136.83
200.9 137.82
201.1 138
201.4 138.06
201.3 138.04
203.6 140.04
204.6 142.44
209.5 145.47
208.6 144.34
210.7 146.3
211.9 147.54
212.2 147.8


Here is my code:
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.IO;

 namespace WindowsFormsApplication1
 {

 public partial class Regression_Form : Form
 {
 public const int maxobs = 50;

 public Regression_Form()
 {
 InitializeComponent();

 }

 private void Btn_Regression_Click(object sender, EventArgs e)
 {
 int n = 0;
 string[] newbits = new string[2];
 double[] xs = new double[maxobs]; //x values
 double[] ys = new double[maxobs]; //y values
 string fileline, listline;
 double xbar, ybar;
 Forbes_Data.Items.Add("x y");

 //Read in the forbes data and parse each line into it's x and y values

 StreamReader sr = new StreamReader("C:\\Forbes\\Forbes_One_Space.txt");

 //Read the first line of text
 fileline = sr.ReadLine();
 string[] bits = fileline.Split(' ');
 xs[n] = double.Parse(bits[0]);
 ys[n] = double.Parse(bits[1]);
 xbar = xs[n];
 ybar = ys[n];
 listline = xs[n].ToString("F") + " " + ys[n].ToString("F");
 Forbes_Data.Items.Add(listline);
 n++;

 //Read the rest of the data, line by line and parse them.
 //Calculate means

 while (fileline != null)
 {

 fileline = sr.ReadLine();
 if (fileline != null)
 {
 newbits = fileline.Split(' ');
 xs[n] = double.Parse(newbits[0]);
 ys[n] = double.Parse(newbits[1]);
 listline = xs[n].ToString("F") + " " + ys[n].ToString("F");
 Forbes_Data.Items.Add(listline);
 xbar += xs[n];
 ybar += ys[n];
 n++;
 }
 }

 //Close file. Calculate and display means.

 sr.Close();
 //xbar = xbar / n;
 //ybar = ybar / n;
 //MeanX.Text = xbar.ToString("F");
 //MeanY.Text = ybar.ToString("F");
 
 

 }
 
For future reference, please always format your code snippets for easy reading. I have added the appropriate tags to your post but that only helps so much because you have managed to remove all the indenting, which is the most important part of making code readable. Please copy code directly from the IDE into formatting tags for best results.

As for the issue, if you're getting a FormatException on that line then obviously the data you're using on that line is not in a valid format to be parsed as a double. Have you actually looked to see what the data is? If you have an issue using specific data then the obvious first step should be to look at the data. Don't just assume that it is what it's supposed to be because, if that were the case, then you wouldn't have an issue.
 
If your computers system culture doesn't use "." as decimal separator then double.Parse throws FormatException when parsing a number like "194.5". In such case you can use a CultureInfo as argument to the Parse call, for example a specific culture that uses such decimal separator or the CultureInfo.InvariantCulture (or NumberFormatInfo.InvariantInfo) that is based on English culture.
Double.Parse Method (String, IFormatProvider) (System)
 
Thanks for the quick responses. I got my program to work. (It was reading past the end of the file, so I just added a do { ... } while loop to prevent this.) I apologize for not formatting my code. (It fits a trendline to data and shows regression statistics.) If anyone wants to see the final version of my program, I can post it.

Regards,

- Tom
 
It was reading past the end of the file, so I just added a do { ... } while loop to prevent this.

I think you mean that it was reading past the end of the text, which implies that you had a line break after the last data item, which is not unusual. In that case, you could have just changed this:
while (fileline != null)
to this:
while (!string.IsNullOrEmpty(fileline)
 
Back
Top Bottom