消息=输入字符串格式不正确。在c#中

时间:2018-04-29 06:50:04

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

2 个答案:

答案 0 :(得分:0)

在csv文件中的某处,有一个无法转换为double的值 要避免此异常,请将Convert.ToDouble替换为Double.TryParse。如果值可以解析为truedouble,则会返回false

话虽如此,你的其余代码没有意义。 如果你看一下这部分,例如:

                while (Convert.ToDouble(testY) >= firstY)//where error is
                {
                    firstY = Convert.ToDouble(testY);

                    if (firstY == Convert.ToDouble(testY))
                    {
                        break;
                    }

假设转换为double是正常的,那么您的代码会检查firstY == Convert.ToDouble(testY)是否始终返回true,因为firstYConvert.ToDouble(testY)的结果,所以您要突破循环,永远不会进入第二个循环,因为它的条件是while (Convert.ToDouble(testY) < firstY) - 你现在应该知道它们是相同的值......

我猜你在代码中混合了testYtestX

答案 1 :(得分:0)

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

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