Message=Input string was not in a correct format. in c#

0

The following exception is being thrown in the first while loop that I convert to double:

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

I'm not sure what I'm writing wrong or how to fix this issue. I'm reading from a csv file. I have tested to make sure the data is being passed from variable to variable, and it is. the data is being read by my streamreader and stored in single dimensional arrays which are being saved to a variable that I am using to compare the data points to determine all the peaks and the valleys in the dataset

This is my program:

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);
                }
            }
        }
    }
}
c#
asked on Stack Overflow Apr 29, 2018 by Dan King • edited Apr 29, 2018 by MichaƂ Turczyn

2 Answers

0

Somewhere in your csv file there's a value that can't be converted to double.
To avoid this exception replace all your Convert.ToDouble with Double.TryParse. That will return true if the value can be parsed to double or false if it can't.

Having said that, the rest of your code doesn't make sense. If you look at this part, for example:

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

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

Assuming the conversion to double was OK, your code checks if firstY == Convert.ToDouble(testY) - which will always return true since firstY is the result of Convert.ToDouble(testY), so you break out of the loop, never entering the second loop since it's condition is while (Convert.ToDouble(testY) < firstY) - you should know by now they are the same value...

I'm guessing you are mixing testY and testX in your code.

answered on Stack Overflow Apr 29, 2018 by Zohar Peled
0

This was the piece of code I needed to keep the exception from being thrown (For anyone who may be receiving the same exception and unsure why) You need to put the conversion of the two variables in a "try/catch". Like this:

try
{
    testX = Convert.ToDouble(values[0]);
    testX = Convert.ToDouble(values[1]);
}
catch
{
}
answered on Stack Overflow May 1, 2018 by Dan King

User contributions licensed under CC BY-SA 3.0