CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2011
    Posts
    12

    convert string array to double array

    I am trying to convert string array to a double array. I am not succeeding as I am getting the exeption of System.FormatException was unhandled
    Message=Input string was not in a correct format.
    Source=mscorlib
    StackTrace:
    at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
    at System.Double.Parse(String s)
    at test1.Form1.PopulateChart() in C:\Documents and Settings\Jonas\my documents\visual studio 2010\Projects\test1\test1\Form1.cs:line 98
    at test1.Form1.buttonAddPoints_Click(Object sender, EventArgs e) in C:\Documents and Settings\Jonas\my documents\visual studio 2010\Projects\test1\test1\Form1.cs:line 24
    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 test1.Program.Main() in c:\documents and settings\jonas\my documents\visual studio 2010\Projects\test1\test1\Program.cs:line 18
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:


    Code:
     
    //Read the rest of the data in the file
    
                    DataTable dtData = new DataTable();
    
                    string AllData = sr.ReadToEnd();
                    string[] rows = AllData.Split("\r".ToCharArray());
    
                    
                    foreach (string row in rows)
                    {
                        string[] items = row.Replace("\r", "").Replace("  ", " ").Trim();
    
                        foreach (string item in items)
                        { 
                        double[] arrDouble = new double[items.Length];
                            for (int i = 0; i < items.Length; i++)
                            {
                            arrDouble[i] = double.Parse(items[i]);
                            }
                        }
                        
                       dtData.Rows.Add(arrDouble);
                    }

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: convert string array to double array

    Post a sample of the string array which you are trying to convert.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: convert string array to double array

    As boudino requested, what are the string values of item[0], item[1], etc.. that you are attempting to parse?

    In addition...

    Take another look at your loops...

    Code:
                         foreach (string item in items)
                        { 
                            double[] arrDouble = new double[items.Length];
                            for (int i = 0; i < items.Length; i++)
                            {
                            arrDouble[i] = double.Parse(items[i]);
                            }
                        }
    you are going through each string item, and for each one of those, you are re-filling the same array of doubles by looping through the string items again. In other words:

    If you have three items "100", "200", "300" , you are basically following this logic...

    Code:
    For item "100"
       Create an array of doubles.
       For item "100"
          Add value 100 to array of doubles.
       For item "200"
          Add value 200 to array of doubles.
       For item "300"
          Add value 300 to array of doubles.
    For item "200"
       Create an array of doubles.
       For item "100"
          Add value 100 to array of doubles.
       For item "200"
          Add value 200 to array of doubles.
       For item "300"
          Add value 300 to array of doubles.
    For item "300"
       Create an array of doubles.
       For item "100"
          Add value 100 to array of doubles.
       For item "200"
          Add value 200 to array of doubles.
       For item "300"
          Add value 300 to array of doubles.
    If look at it, you are creating the same array of double values three times... one too many loops.

    Plus, because you initializing your array of doubles within a loop, and trying to actually use it (to fill your datarow) outside of that loop... you may have an issue there.
    Last edited by fcronin; June 10th, 2011 at 12:42 PM.

  4. #4
    Join Date
    May 2011
    Posts
    12

    Re: convert string array to double array

    it's a file, see attached. first line goes as a legend for my columns, so disregard them, I omitted some code
    Attached Files Attached Files

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: convert string array to double array

    Quote Originally Posted by jbaltrus View Post
    it's a file, see attached. first line goes as a legend for my columns, so disregard them, I omitted some code
    I would disregard them, except it looks like your code is not. Looks like you are trying to parse the column headers as doubles. Learn to use the debugger, it will save you a lot of time.

    You should skip the first line, i.e.,

    Code:
    foreach( var row in rows.Skip(1) )
    {
        // processing here
    }
    Last edited by BigEd781; June 10th, 2011 at 06:21 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured