[英]Message=Input string was not in a correct format. in c#
我轉換為double的第一個while循環中引發以下異常:
System.FormatException
HResult=0x80131537
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToDouble(String value)
at WindowsFormsApp6.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\King\source\repos\WindowsFormsApp6\WindowsFormsApp6\Form1.cs:line 52
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApp6.Program.Main() in C:\Users\King\source\repos\WindowsFormsApp6\WindowsFormsApp6\Program.cs:line 19
我不確定自己寫錯了什么或如何解決此問題。 我正在讀取一個csv文件。 我已經進行測試,以確保數據從一個變量傳遞到另一個變量。 數據被流讀取器讀取,並存儲在一維數組中,這些數組被保存到一個變量中,用於比較數據點以確定數據集中的所有峰和谷
這是我的程序:
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 WindowsFormsApp6
{
public partial class Form1 : Form
{
double firstY = 0.0;
string testX;
string testY;
string[] xpoint = new string[5000];
string[] ypoint = new string[5000];
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var reader = new StreamReader(@"D:\data.csv"))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
var values = line.Split(',');
testX = values[0];
testY = values[1];
if (firstY == 0.0)
{
firstY = Convert.ToDouble(testY);
Convert.ToString(testY);
}
while (Convert.ToDouble(testY) >= firstY)//where error is
{
firstY = Convert.ToDouble(testY);
if (firstY == Convert.ToDouble(testY))
{
break;
}
if (Convert.ToDouble(testY) < firstY)
{
listBox1.Items.Add(Convert.ToDouble(testX) + "," + firstY);
break;
}
break;
}
while (Convert.ToDouble(testY) < firstY)
{
firstY = Convert.ToDouble(testY);
if(firstY == Convert.ToDouble(testY))
{
break;
}
if (Convert.ToDouble(testY) > firstY)
{
listBox2.Items.Add(Convert.ToDouble(testX) + "," + firstY);
break;
}
break;
}
Convert.ToString(testX);
Convert.ToString(testY);
}
}
}
}
}
csv文件中的某個地方存在一個無法轉換為雙精度值的值。
為避免此異常,請用Double.TryParse
替換所有的Convert.ToDouble
。 如果該值可以解析為double
則返回true
否則,則返回false
。
話雖如此,其余的代碼沒有任何意義。 如果您看這部分,例如:
while (Convert.ToDouble(testY) >= firstY)//where error is
{
firstY = Convert.ToDouble(testY);
if (firstY == Convert.ToDouble(testY))
{
break;
}
假設轉換為double可以,那么您的代碼將檢查firstY == Convert.ToDouble(testY)
,由於firstY
是Convert.ToDouble(testY)
的結果,因此它將始終返回true
,因此您會跳出循環,從不進入第二個循環,因為它的條件是while (Convert.ToDouble(testY) < firstY)
-您現在應該知道它們是相同的值...
我猜您在代碼中混用了testY
和testX
。
這是我需要防止拋出異常的代碼(對於可能接收相同異常並且不確定原因的任何人),您需要將兩個變量的轉換放在“ try / catch”中。 像這樣:
try
{
testX = Convert.ToDouble(values[0]);
testX = Convert.ToDouble(values[1]);
}
catch
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.