0

So this time, I've got numbers entered in as a list, with a space delimiting each number. The code I've written now places the number in a row as it should, but fails out when I try and convert the string to Int32, killing the program and not giving me the sum. I don't understand errors well enough yet to be able to decipher exactly what the error is. How does a guy convert split string arrays into numbers to produce a sum?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Dynamic_Entry
    {
    class Program
     {
    static void Main()
    {
        Console.Write("Please provide a list of numbers, separated by spaces: ");
        string list = Console.ReadLine();
        string[] parts = list.Split(' ');
        int sum = 0;

        for (int i = 0; i < parts.Length ; i++)
        {
            Console.WriteLine("{0, 5}", parts[i]);

        }

        sum = Convert.ToInt32(list);
        Console.WriteLine("-----");
        Console.Write("{0, 5}", sum);
        Console.ReadLine();

        }
      }
    }
0

2 Answers 2

5
for (int i = 0; i < parts.Length; i++)
{
    Console.WriteLine("{0, 5}", parts[i]);
    sum += Convert.ToInt32(parts[i]);
}

Fixed.

You were trying to convert "1 2 3 4 5 55" to an int. You must convert "1", "2, "3"... to an int and add them to sum.

I'll add that if you want to Split the string, it would be better to do something like

string[] parts = list.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

In this way multiple spaces between numbers are removed (1     2 3 for example)

Andrei had posted a very simple example of use of LINQ...

int sum = parts.Sum(p => Convert.ToInt32(p));

This you would put OUTSIDE the for cycle. It converts to int and adds all the "parts". It means "for each part convert it to int and add it. Return the sum".

Sign up to request clarification or add additional context in comments.

4 Comments

Wow! Thanks for the quick response. However, can you explain how sum makes it out of the for loop? I thought the for loop was kinda like Vegas...you know, what happens in the loop, stays in the loop. How is the program able to grab the sum out of the loop when the loop completes?
sum is defined outside the loop
int sum = parts.Sum(p => Convert.ToInt32(p)); This will give wrong result since "p" will be a "char" data type.
@BimalDas ???? parts is string[] because it is the result of a string.Split
2

You can convert each string to an int and add them in a loop as @xanatos proposes, or you can use LINQ and Enumerable.Sum(), eg:

var sum=parts.Sum(part=>Convert.ToInt32(part));

or

var sum=parts.Select(part=>Convert.ToInt32(part))
     .Sum();

The real benefit comes when you have more complex expressions, eg. when you need to filter values, extract properties etc.

For example, you could filter values greater than 3 like this:

var sum=parts.Select(part=>Convert.ToInt32(part))
     .Where(num=>num>3)
     .Sum();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.