-1

I'm new to C# programming. I was wondering how do you take multiple inputs in one line. My code:

using System;

namespace ArrayExercise01
{
    class Program
    {
        static void Main(string[] args)
        {
            int arraySize = int.Parse(Console.ReadLine());
            int[] ar = new int[arraySize];
            int i;
            for(i=0; i<arraySize; i++) 
            {
                Console.Write("");
                ar[i] = int.Parse(Console.ReadLine());
            }
            int sum = 0;
            foreach (int arrayElement in ar)
            {
                sum += arrayElement;
            }
            Console.WriteLine(sum);
        }
    }
}

I get this output after running this:

3
2
3
4
9

But I wanted to do something like this (Multiple Input in single line):

3
2 3 4
9

3 Answers3

1

Instead of:

ar[i] = int.Parse(Console.ReadLine());

You could:

foreach(string s in Console.ReadLine().Split()){
  ar[i] = int.Parse(s);
  i++;
}

Split without any arguments splits on whitespace, of which is one.. So the user is prompted for a string , and they could enter 1 or 1 2 3 4 5 or any combination (though there will be a problem if they enter more numbers than the remaining array can hold). The code splits them on the space, to become five strings of "1" "2" "3" "4" "5", and then the loop runs 5 times, and each one is parsed in turn and stored in an array index. The index i is incremented each time

As well as checking that i is still within the length of the array, it would be advisable to skip bad input too; take a look at int.TryParse, which returns a boolean false if the parsing did not succeed; if it returns false, don't add to the array/don't increment i

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
1

As per your requirement of "take multiple input on same line" you could do this as follow

List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();

but there is a problem while we take multiple input on same line as "we can't restrict user to enter only given size of numbers". To avoid this we can give informative message(or we can say validation message) to user as follows

while (numbers.Any() && ((numbers.Count() < arraySize) || (numbers.Count() > arraySize)))
{
  Console.WriteLine("You have enter the numbers might be less than or greater than given size");
  Console.WriteLine("Please the numbers of given size");
  numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
}

Finally, you can do sum as follows

int sum = 0;
foreach (var num in numbers)
{
  sum += num;
}
//or you can use directly sum method as follows
//Console.WriteLine(numbers.Sum());

Program as follows

static void Main(string[] args)
    {
        int arraySize = int.Parse(Console.ReadLine());

        List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();

        while (numbers.Any() && ((numbers.Count() < arraySize) || (numbers.Count() > arraySize)))
        {
            Console.WriteLine("You have enter the numbers might be less than or greater than given size");
            Console.WriteLine("Please the numbers of given size");
            numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
        }

        int sum = 0;
        foreach (var num in numbers)
        {
            sum += num;
        }
        //or you can use directly sum method as follws
        //Console.WriteLine(numbers.Sum());

        Console.WriteLine(sum);
        Console.Read();
    }

Results:

3
1 2
You have enter the numbers might be less than or greater than given size
Please the numbers of given size
1 2 3 4
You have enter the numbers might be less than or greater than given size
Please the numbers of given size
1 2 3
6
Harish
  • 789
  • 1
  • 7
  • 21
0

Try this, but be careful, inputting too many numbers will cause IndexOutOfRangeException.

for (int i = 0; i < arraySize; )
{
    // first catch the input
    string input = Console.ReadLine();

    // split it by space
    string[] everyNumber = input.Split(' ');

    for (int j = 0; j < everyNumber.Length; j++)
    {
        // parse each number in string array
        ar[i] = int.Parse(everyNumber[j]);
        i++;
    }
}
Reodanoe
  • 1
  • 1