0

I simply want to enter the following numbers

100 8
15 245
1945 54

into a value, but for some reason when I copy-paste it into my ReadLine - program kicks me out (with no error or smth - so I cannot barely understand what's just happened...)

I've already a code that allows me to insert a set of numbers when they are arranged in a LINE (but not as a table as shown in the description...)

        int numberOfElements = Convert.ToInt32(Console.ReadLine());   
        int sum = 0;
        string input = Console.ReadLine();     
        var numbers = Array.ConvertAll(input.Split(' '), int.Parse).ToList();   


        Console.ReadKey();

I expect to have my numbers in a List

4
  • At which Console.ReadLine() are you entering that value? Commented Feb 2, 2019 at 23:58
  • Ah yes, sorry - INPUT Commented Feb 3, 2019 at 0:00
  • The first input (string) "100 8" is not a number, obviously. (Note the whitespace between the 0 and the 8) Thus, your first code line int numberOfElements = Convert.ToInt32(Console.ReadLine()); will already fail. Commented Feb 3, 2019 at 0:00
  • If you don't see the exception, i guess you use a try-catch clause with an empty catch block. If this is indeed the case, then DON'T DO THAT. A try-catch with an empty catch block would be equivalent to this here: youtube.com/watch?v=pdFl__NlOpA. (That said, i am just speculating here about what you do in your code...) Commented Feb 3, 2019 at 0:04

3 Answers 3

1

Console.ReadLine() only reads one line. string input = Console.ReadLine() reads first line When you get into new line. In your case only first line is read then for the second line your program only gets first character and exits.

Check This:

    int numberOfElements = Convert.ToInt32(Console.ReadLine());   

    int sum= 0;
    for (int i=0; i< numberOfElements; i++)
    {

        string input = Console.ReadLine();     
        sum += Array.ConvertAll(input.Split(' '), int.Parse).Sum();
    }

    Console.WriteLine(sum);

Working Fiddle

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

Comments

1

Obviously when you paste in Carriage returns, ReadLine only takes up to the first Carriage Return, you will need a loop of some description

int numberOfElements = Convert.ToInt32(Console.ReadLine());

var sb = new StringBuilder();

for (int i = 0; i < numberOfElements; i++)
{
   Console.WriteLine($"Enter value {i+1}");
   sb.AppendLine(Console.ReadLine());
}

var input = sb.ToString();

// do what ever you want here

Console.ReadKey();

Comments

0

I'm assuming that you're looking for a way to allow a user to paste something from another source into your console program, so you're looking for an answer where you can process a multi-line string input from the user (where they paste a string that contains one or more newline characters).

If this is the case, then one way to do this would be to check the value of Console.KeyAvailable after the first call to ReadLine to see if there's still more input in the buffer, and if there is, add it to the input you've already captured.

For example, here's a method that takes in a prompt (to display to the user), and then returns a List<string> that contains an entry for each row the user pasted (or typed):

private static List<string> GetMultiLineStringFromUser(string prompt)
{
    Console.Write(prompt);

    // Create a list and add the first line to it
    List<string> results = new List<string> { Console.ReadLine() };

    // KeyAvailable will return 'true' if there is more input in the buffer
    // so we keep adding the lines until there are none left
    while(Console.KeyAvailable)
    {
        results.Add(Console.ReadLine());
    }

    // Return the list of lines
    return results;
}

In use, this might look something like:

private static void Main()
{
    var input = GetMultiLineStringFromUser("Paste a multi-line string and press enter: ");

    Console.WriteLine("\nYou entered: ");
    foreach(var line in input)
    {
        Console.WriteLine(line);
    }

    GetKeyFromUser("\nDone!\nPress any key to exit...");
}

Output

enter image description here

What you do next depends on what you want to accomplish. If you want to take all the lines, split them on the space character, and return all the results as a list of individual integers, you could do something like this:

private static void Main()
{
    int temp = 0;

    List<int> numbers =
        GetMultiLineStringFromUser("Paste a multi-line string and press enter: ")
            .SelectMany(i => i.Split()) // Get all the individual entries
            .Where(i => int.TryParse(i, out temp)) // Where the entry is an int
            .Select(i => Convert.ToInt32(i)) // And convert the entry to an int
            .ToList();

    Console.WriteLine("\nYou entered: ");
    foreach (var number in numbers)
    {
        Console.WriteLine(number);
    }

    GetKeyFromUser("\nDone!\nPress any key to exit...");
}

Output enter image description here

Or you could even do something fancy like:

Console.WriteLine($"\n{string.Join(" + ", numbers)} = {numbers.Sum()}");

Output

![enter image description here

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.