Hello again,

I am having some issues adding up numbers. Here is the logic:

I have a long string that is in the format name|1|2|3 etc.

I am trying to add the sum of all the numbers following the name. So, from the above example, I am looking for the number 6, 1+2+3.

At first I was thinking that, since the first value is a name, that was what is giving me the cannot convert type string to int, but then I thought that this error would happen after compiling and not before.

I was able to determine the number of grades, successfully, in the string, since that will not be constant with this code:

string temp = StudentsListBox.SelectedItem.ToString();
// Determine Number of Grades
            int totalIndexes = 0;
            int i = 0;
            while (temp.IndexOf("|",i) != temp.LastIndexOf("|"))
            {
                i = temp.IndexOf("|", i);
                // Prevent infinite loop
                i++;
                totalIndexes++;
            }
            totalIndexes += 1;
            ScoreCountTextBox.Text = totalIndexes.ToString();


//------------Having Issues Here------------
// Determine Total Grade
            int totalGrade = 0;
            int p = 0;
            int i = 0;
            while (temp.IndexOf("|", i) != temp.LastIndexOf("|"))
            {
                i = temp.IndexOf("|", i);
                p = temp.IndexOf("|", i+1);
                totalGrade += (int)temp.Substring(i + 1, p - i).ToString();
                // Prevent infinite loop
                i = i + 1;
            }

Thank you for taking the time to read this.

Edited 6 Years Ago by sarsekim: Fixed Code Wrapping

You do not cast strings to int in such a manner. Your options boil down to these:

Convert.ToInt32([type] input)
int.Parse(string input)
int.TryParse(string input, out int result)

Of these, the first 2 will throw exceptions if you do not provide a valid input that can be converted to an integer. The third will not throw an exception, as it returns a boolean indicating whether or not the conversion is successful. If it is successful, the value will be stored in the out parameter result . If not, result will be set to the default value of an int, which is 0.

Of the three methods, it is recommended that you go with the third since throwing exceptions for invalid inputs is far less efficient than getting a true/false execution path. Here is an example.

string input = "A";
int output = 0;

if (int.TryParse(input, out output))
{
    // control flows here if input is an integer
    // "output" will be the value of the converted input
}
else
{
    // control flows here if input is not an integer
    // "output" will be 0
}

As for this particular problem, I have multiple ideas on how to solve it a bit more elegantly than you are currently doing. However, being that this looks like homework, I think it's good that you're sorting this out for yourself. I will, however, offer a slight coding hint that could make your program a bit easier.

string line = "Bob|100|92|96";
string[] values = line.Split('|');
// splits input into array containing Bob, 100, 92, 96

Edited 6 Years Ago by apegram: n/a

Comments
exactly what i would have said ;)
Very neat explanation :)

The detail in your post is amazing, thank you!

Haha, I first tried your third selection, it compiled fine, but always returned zero for the total grades. I messed around with my logic a little bit with no luck, then I tried your last sujestion, split, and I fixed it completely, and rewrote my other code with it, smaller, neater, etc, thanks.

Now if i can just finish finish this before midnight.... Don't see that happening haha.

Thank you again!


You do not cast strings to int in such a manner. Your options boil down to these:

Convert.ToInt32([type] input)
int.Parse(string input)
int.TryParse(string input, out int result)

Of these, the first 2 will throw exceptions if you do not provide a valid input that can be converted to an integer. The third will not throw an exception, as it returns a boolean indicating whether or not the conversion is successful. If it is successful, the value will be stored in the out parameter result . If not, result will be set to the default value of an int, which is 0.

Of the three methods, it is recommended that you go with the third since throwing exceptions for invalid inputs is far less efficient than getting a true/false execution path. Here is an example.

string input = "A";
int output = 0;

if (int.TryParse(input, out output))
{
    // control flows here if input is an integer
    // "output" will be the value of the converted input
}
else
{
    // control flows here if input is not an integer
    // "output" will be 0
}

As for this particular problem, I have multiple ideas on how to solve it a bit more elegantly than you are currently doing. However, being that this looks like homework, I think it's good that you're sorting this out for yourself. I will, however, offer a slight coding hint that could make your program a bit easier.

string line = "Bob|100|92|96";
string[] values = line.Split('|');
// splits input into array containing Bob, 100, 92, 96
This article has been dead for over six months. Start a new discussion instead.