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