Reading text file problem with casting to double
-
I've trying to read a .txt files in order to plot the data on a ZedGraph. The problem is I cannot convert the data to double. The compiler gives bunch of errors(Error 1 Cannot convert type 'string' to 'double'). The following is a piece of code that i'm having trouble with. private void BRead(GraphPane myPane) { FileStream fleReader = new FileStream("kel.txt", FileMode.Open, FileAccess.Read); StreamReader stmReader = new StreamReader(fleReader); StringBuilder lstChars = new StringBuilder(); double x = 0, y; int iChar = stmReader.Read(); lstChars.Append((char)iChar); // double[] yx = new double[lstChars.Length]; PointPairList list = new PointPairList(); while (iChar > -1) { x = x + (double)0.0002; iChar = stmReader.Read(); lstChars.Append((char)iChar); y = (double)(lstChars.ToString()); //error list.Add(x, y); } this.textBox1.Text = lstChars.ToString(); stmReader.Close(); CurveItem myCurve = myPane.AddCurve("My Curve",list, Color.Red, SymbolType.Diamond); } can anyone help me please!!
-
I've trying to read a .txt files in order to plot the data on a ZedGraph. The problem is I cannot convert the data to double. The compiler gives bunch of errors(Error 1 Cannot convert type 'string' to 'double'). The following is a piece of code that i'm having trouble with. private void BRead(GraphPane myPane) { FileStream fleReader = new FileStream("kel.txt", FileMode.Open, FileAccess.Read); StreamReader stmReader = new StreamReader(fleReader); StringBuilder lstChars = new StringBuilder(); double x = 0, y; int iChar = stmReader.Read(); lstChars.Append((char)iChar); // double[] yx = new double[lstChars.Length]; PointPairList list = new PointPairList(); while (iChar > -1) { x = x + (double)0.0002; iChar = stmReader.Read(); lstChars.Append((char)iChar); y = (double)(lstChars.ToString()); //error list.Add(x, y); } this.textBox1.Text = lstChars.ToString(); stmReader.Close(); CurveItem myCurve = myPane.AddCurve("My Curve",list, Color.Red, SymbolType.Diamond); } can anyone help me please!!
I've never used .net, but surely you want to convert the string to double, the ToString() function seems to convert to a String.
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
I've trying to read a .txt files in order to plot the data on a ZedGraph. The problem is I cannot convert the data to double. The compiler gives bunch of errors(Error 1 Cannot convert type 'string' to 'double'). The following is a piece of code that i'm having trouble with. private void BRead(GraphPane myPane) { FileStream fleReader = new FileStream("kel.txt", FileMode.Open, FileAccess.Read); StreamReader stmReader = new StreamReader(fleReader); StringBuilder lstChars = new StringBuilder(); double x = 0, y; int iChar = stmReader.Read(); lstChars.Append((char)iChar); // double[] yx = new double[lstChars.Length]; PointPairList list = new PointPairList(); while (iChar > -1) { x = x + (double)0.0002; iChar = stmReader.Read(); lstChars.Append((char)iChar); y = (double)(lstChars.ToString()); //error list.Add(x, y); } this.textBox1.Text = lstChars.ToString(); stmReader.Close(); CurveItem myCurve = myPane.AddCurve("My Curve",list, Color.Red, SymbolType.Diamond); } can anyone help me please!!
Replace: y = (double)(lstChars.ToString()); //error with: y = Convert.ToDouble(lstChars.ToString()); You are attempting to perform a cast that is not implicit. You need to use the ConvertTo function to handle this type of cast. Also, if the string does not represent a double or has characters in it that would not be used to represent a double the cast will fail.
-
Replace: y = (double)(lstChars.ToString()); //error with: y = Convert.ToDouble(lstChars.ToString()); You are attempting to perform a cast that is not implicit. You need to use the ConvertTo function to handle this type of cast. Also, if the string does not represent a double or has characters in it that would not be used to represent a double the cast will fail.
-
y = Convert.ToDouble(lstChars.ToString()); gives the following error: Input string was not in a correct format. what can I do about this? Thanks for your reply.
This error tells me that the string that you are passing in to be converted contains something other than numbers and a decimal point. For example if you pass in "A32.41" the conversion to double will fail. If you pass in "36.42" the coversion will pass. Look to see what the actual string is when you call the convert function.