Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Been wasting a lot of time on this. I think it 'should' work, but I can't find what's wrong with it.

C#
protected int Checkscore(Node x)
    {
            string[] numbers = x.GetProperty("answers").Value.Split(';');
            int score = 0;
            foreach (string n in numbers)
            {
          //      score += int.Parse(n);
                LabelMonitor.Text += n + " ";
            }
            
            return score;
    }


The property "answers" looks like this: "1;100;1;50;"
And "LabelMonitor" shows this: "1 100 1 50"

So, it should be okay to parse and count them all up right?
Yet, if I uncomment the other line, it throws an exception:

"Input string was not in a correct format."

Does anyone know what I did wrong? I'm going crazy. :-s
Posted

Try this :
C#
if(n!="") score += int.Parse(n);
 
Share this answer
 
Comments
André Kraak 21-Oct-11 13:09pm    
my 5, good catch that the final ';' in the string causes a empty string to be returned by the Split.
Mehdi Gholam 21-Oct-11 13:12pm    
Yep that's it, happened to me a couple of times.
Have you tried Convert.ToInt32(n). I seem to have more luck with that than with int.Parse, especially when converting results from a database.
 
Share this answer
 
The solution given by Mehdi Gholam works, but this alternative also solves your problem.

C#
protected int Checkscore(Node x)
    {
            char[] delimiter = {';'};
            string[] numbers = x.GetProperty("answers").Value.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            int score = 0;
            foreach (string n in numbers)
            {
                score += int.Parse(n);
                LabelMonitor.Text += n + " ";
            }
            
            return score;
    }


The StringSplitOptions.RemoveEmptyEntries[^] option tells the Split[^] function to remove all empty results.



To make sure the code also works when the string contains non digit characters you should use Int32.TryParse[^].
When using TryParse you also deal with the empty result from the Split function, so you can use your original call to the Split function again.
C#
protected int Checkscore(Node x)
    {
            string[] numbers = x.GetProperty("answers").Value.Split(';');
            int score = 0;
            int parseResult = 0;

            foreach (string n in numbers)
            {
		if (Int32.TryParse(n, out parseResult))
                	score += parseResult;
                LabelMonitor.Text += n + " ";
            }
            
            return score;
    }
 
Share this answer
 
v2
Comments
BillWoodruff 21-Oct-11 23:46pm    
+5 for rounding out Mehdi's solution and covering "all the bases."
I've just tried this with the values you showed and it works correctly. Try stepping through your code with the debugger to see where you get an invalid string.
 
Share this answer
 
Comments
André Kraak 21-Oct-11 13:08pm    
Did you include the final ';' in the source string?
Because of this the Split returns a final empty string, which when parsed causes the FormatException.
Richard MacCutchan 21-Oct-11 15:16pm    
Wow, I managed to miss that, foolishly typing the values into my code rather than using Copy & Paste. But, it's yet another reason why TryParse() is always the better choice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900