0

How can I separate a set of numbers in a console app in C# instead of asking the user for each individual number? E.g. instead of doing this...

        double first, second, third, fourth;

        Console.Write("Please enter first digit: ");
        first = Convert.ToDouble(Console.ReadLine());
        Console.Write("Please enter second digit: ");
        second = Convert.ToDouble(Console.ReadLine());
        Console.Write("Please enter third digit: ");
        third = Convert.ToDouble(Console.ReadLine());
        Console.Write("Please enter fourth digit: ");
        fourth = Convert.ToDouble(Console.ReadLine());
1
  • 1
    If its just single digits then you could use a string which is an array of chars. I doubt they will only be single digits tho. 2 cents. Commented Feb 22, 2013 at 4:13

3 Answers 3

5

You can have them enter all digits at once with a separator of some sort (space in this example).

Console.Write("Please enter a bunch of digits separated by a space: ");
var allDigits = Console.ReadLine().Split(' ');
Double[] digits = allDigits.Select(d => Covert.ToDouble(d)).ToArray();

If your requirement is limited to 4 inputs restrict the allDigit by using IEnumerable<string>.Take(4)

Console.Write("Please enter a bunch of digits separated by a space: ");
var allDigits = Console.ReadLine().Split(' ').Take(4);
Double[] digits = allDigits.Select(d => Covert.ToDouble(d)).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need Split(' '), just Split()
@CuongLe, true enough - but as an example I try to make it obvious how the separator can be changed.
2

Make use of array, it can help you to faster read/write operation into variables

    double[] numbers = new double[4];

    for (int i = 0; i < 4; i++)
    {
        Console.WriteLine("Enter {0} of 4 Number : ", i + 1);
        numbers[i] = Convert.ToDouble(Console.ReadLine());
    }

    // numbers[0] = first
    // numbers[1] = second
    // numbers[2] = third
    // numbers[3] = fourth

If you really want to use four variables then, this can be the most short way :

    double first, second, third, fourth;

    for (int i = 1; i <= 4; i++)
    {
        Console.WriteLine("Enter a number : ");
        double input = Convert.ToDouble(Console.ReadLine());
        switch (i)
        {

            case 1:
                first = input;
                break;

            case 2:
                second = input;
                break;

            case 3:
                third = input;
                break;
            case 4:
                fourth = input;
                break;

        }
    }

Comments

1

Here's one way:

Console.Write("Please enter numbers, comma-separated: ");
var numbers = Console.ReadLine()
    .Split(',')
    .Select(x => Double.Parse(x.Trim()))
    .ToList();

In real-life code though it would probably be better to use TryParse and throw an error back to the user.

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.