locked
Input string was not in a correct format RRS feed

  • Question

  • I'm getting an error when I run my program. The line that has an error is this:

    int[] line = Array.ConvertAll<string, int> (lines, int.Parse);

    not sure how to fix it
    Tuesday, April 17, 2012 12:21 AM

Answers

  • string[] lines = ...;
    lines.Select(line => line.Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries))
    .GroupBy(line => line[0])
    .Select(group => new
    {
      letter = group.Key,
      FirstValue = group.Sum(line => int.Parse(line[1])),
      SecondValue = group.Sum(line => int.Parse(line[2])),
    });



    • Edited by servy42 Tuesday, April 17, 2012 4:06 PM
    • Marked as answer by seeh243 Wednesday, April 18, 2012 2:18 AM
    Tuesday, April 17, 2012 3:32 PM

All replies

  • Do you want to convert a string, or string array to integer array?

    Mitja

    Tuesday, April 17, 2012 3:19 AM
  • In any case, you can use Select extension method:

    string[] strArr = { "1", "2", "3" };
    string str = "1,2,3";
    int[] intArr = strArr.Select(s => int.Parse(s)).ToArray(); //convert string[]
    int[] intArr2 = str.Split(',').Select(s => int.Parse(s)).ToArray(); //convert string
    


    Mitja

    Tuesday, April 17, 2012 3:26 AM
  • Check the content of lines. At list one item is not convertible to an int.
    • Proposed as answer by servy42 Tuesday, April 17, 2012 2:20 PM
    Tuesday, April 17, 2012 7:31 AM
  • I'm trying to convert a line that contains something like this:

    E,1,100

    Tuesday, April 17, 2012 1:45 PM
  • Well, you can't cast the letter "E" to an integer!  If you want to just throw away non-integer strings, you could tweak the anonymous method like so:

    string[] strArr = { "1", "2", "3" };
    string str = "1,2,3";
    int?[] intArr = strArr.Select(s => 
    {int val; 
     if (int.TryParse(s, out val) return new int?(val); else return new int?();
     }).ToArray(); //convert string[]
    If you don't want ot just throw away the letters, then you'll need to figure out how to handle them.
    Tuesday, April 17, 2012 1:59 PM
  • or:

                string str = "E,1,100";
                int[] intArr = str.Split(',').Select(s =>
                {
                    int value;
                    bool success = int.TryParse(s.ToString(), out value);
                    return new { value, success };
                })
                      .Where(w => w.success)
                      .Select(s => s.value).ToArray();


    Mitja

    Tuesday, April 17, 2012 2:16 PM
  • I don't want to throw away the letters. What I'm trying to do is read the line of data and if it begins with a certain letter to place the third number into a category:

    Ex.

    E, 1, 100

    H, 1, 100

    E, 2, 80

    E array is 180

    H array is 100

    I had if statements after to check if the first value was a certain letter and if so then the third value would then place that into its own array
    • Edited by seeh243 Tuesday, April 17, 2012 3:26 PM
    Tuesday, April 17, 2012 3:23 PM
  • string[] lines = ...;
    lines.Select(line => line.Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries))
    .GroupBy(line => line[0])
    .Select(group => new
    {
      letter = group.Key,
      FirstValue = group.Sum(line => int.Parse(line[1])),
      SecondValue = group.Sum(line => int.Parse(line[2])),
    });



    • Edited by servy42 Tuesday, April 17, 2012 4:06 PM
    • Marked as answer by seeh243 Wednesday, April 18, 2012 2:18 AM
    Tuesday, April 17, 2012 3:32 PM
  • I don't want to throw away the letters. What I'm trying to do is read the line of data and if it begins with a certain letter to place the third number into a category:

    ...

    I had if statements after to check if the first value was a certain letter and if so then the third value would then place that into its own array

    What a poor descrpition. Can you finally create a full issue explanation? In each of your posts you add  bit of "something-else". 

    What means "you add 3rd number into category? What is category here?

    Please be punctual!


    Mitja

    Tuesday, April 17, 2012 4:16 PM