Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Reading text file problem with casting to double

Reading text file problem with casting to double

Scheduled Pinned Locked Moved Algorithms
help
5 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kelvinic
    wrote on last edited by
    #1

    I've trying to read a .txt files in order to plot the data on a ZedGraph. The problem is I cannot convert the data to double. The compiler gives bunch of errors(Error 1 Cannot convert type 'string' to 'double'). The following is a piece of code that i'm having trouble with. private void BRead(GraphPane myPane) { FileStream fleReader = new FileStream("kel.txt", FileMode.Open, FileAccess.Read); StreamReader stmReader = new StreamReader(fleReader); StringBuilder lstChars = new StringBuilder(); double x = 0, y; int iChar = stmReader.Read(); lstChars.Append((char)iChar); // double[] yx = new double[lstChars.Length]; PointPairList list = new PointPairList(); while (iChar > -1) { x = x + (double)0.0002; iChar = stmReader.Read(); lstChars.Append((char)iChar); y = (double)(lstChars.ToString()); //error list.Add(x, y); } this.textBox1.Text = lstChars.ToString(); stmReader.Close(); CurveItem myCurve = myPane.AddCurve("My Curve",list, Color.Red, SymbolType.Diamond); } can anyone help me please!!

    C C 2 Replies Last reply
    0
    • K kelvinic

      I've trying to read a .txt files in order to plot the data on a ZedGraph. The problem is I cannot convert the data to double. The compiler gives bunch of errors(Error 1 Cannot convert type 'string' to 'double'). The following is a piece of code that i'm having trouble with. private void BRead(GraphPane myPane) { FileStream fleReader = new FileStream("kel.txt", FileMode.Open, FileAccess.Read); StreamReader stmReader = new StreamReader(fleReader); StringBuilder lstChars = new StringBuilder(); double x = 0, y; int iChar = stmReader.Read(); lstChars.Append((char)iChar); // double[] yx = new double[lstChars.Length]; PointPairList list = new PointPairList(); while (iChar > -1) { x = x + (double)0.0002; iChar = stmReader.Read(); lstChars.Append((char)iChar); y = (double)(lstChars.ToString()); //error list.Add(x, y); } this.textBox1.Text = lstChars.ToString(); stmReader.Close(); CurveItem myCurve = myPane.AddCurve("My Curve",list, Color.Red, SymbolType.Diamond); } can anyone help me please!!

      C Offline
      C Offline
      cp9876
      wrote on last edited by
      #2

      I've never used .net, but surely you want to convert the string to double, the ToString() function seems to convert to a String.

      Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

      1 Reply Last reply
      0
      • K kelvinic

        I've trying to read a .txt files in order to plot the data on a ZedGraph. The problem is I cannot convert the data to double. The compiler gives bunch of errors(Error 1 Cannot convert type 'string' to 'double'). The following is a piece of code that i'm having trouble with. private void BRead(GraphPane myPane) { FileStream fleReader = new FileStream("kel.txt", FileMode.Open, FileAccess.Read); StreamReader stmReader = new StreamReader(fleReader); StringBuilder lstChars = new StringBuilder(); double x = 0, y; int iChar = stmReader.Read(); lstChars.Append((char)iChar); // double[] yx = new double[lstChars.Length]; PointPairList list = new PointPairList(); while (iChar > -1) { x = x + (double)0.0002; iChar = stmReader.Read(); lstChars.Append((char)iChar); y = (double)(lstChars.ToString()); //error list.Add(x, y); } this.textBox1.Text = lstChars.ToString(); stmReader.Close(); CurveItem myCurve = myPane.AddCurve("My Curve",list, Color.Red, SymbolType.Diamond); } can anyone help me please!!

        C Offline
        C Offline
        cjengler
        wrote on last edited by
        #3

        Replace: y = (double)(lstChars.ToString()); //error with: y = Convert.ToDouble(lstChars.ToString()); You are attempting to perform a cast that is not implicit. You need to use the ConvertTo function to handle this type of cast. Also, if the string does not represent a double or has characters in it that would not be used to represent a double the cast will fail.

        K 1 Reply Last reply
        0
        • C cjengler

          Replace: y = (double)(lstChars.ToString()); //error with: y = Convert.ToDouble(lstChars.ToString()); You are attempting to perform a cast that is not implicit. You need to use the ConvertTo function to handle this type of cast. Also, if the string does not represent a double or has characters in it that would not be used to represent a double the cast will fail.

          K Offline
          K Offline
          kelvinic
          wrote on last edited by
          #4

          y = Convert.ToDouble(lstChars.ToString()); gives the following error: Input string was not in a correct format. what can I do about this? Thanks for your reply.

          C 1 Reply Last reply
          0
          • K kelvinic

            y = Convert.ToDouble(lstChars.ToString()); gives the following error: Input string was not in a correct format. what can I do about this? Thanks for your reply.

            C Offline
            C Offline
            cjengler
            wrote on last edited by
            #5

            This error tells me that the string that you are passing in to be converted contains something other than numbers and a decimal point. For example if you pass in "A32.41" the conversion to double will fail. If you pass in "36.42" the coversion will pass. Look to see what the actual string is when you call the convert function.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups