Answered by:
Input string was not in a correct format

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 itTuesday, 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])), });
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
- Proposed as answer by kimdbaMicrosoft employee Tuesday, April 17, 2012 6:53 AM
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])), });
Tuesday, April 17, 2012 3:32 PM -
Please check following link, It may be useful to you.Tuesday, April 17, 2012 4:06 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 arrayWhat 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