繁体   English   中英

Message =输入字符串的格式不正确。 在C#中

[英]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) ,由于firstYConvert.ToDouble(testY)的结果,因此它将始终返回true ,因此您会跳出循环,从不进入第二个循环,因为它的条件是while (Convert.ToDouble(testY) < firstY) -您现在应该知道它们是相同的值...

我猜您在代码中混用了testYtestX

这是我需要防止抛出异常的代码(对于可能接收相同异常并且不确定原因的任何人),您需要将两个变量的转换放在“ try / catch”中。 像这样:

try
{
    testX = Convert.ToDouble(values[0]);
    testX = Convert.ToDouble(values[1]);
}
catch
{
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM