0

this snippet gets a list from user and displays it.Can anyone tell how to display all the elements in a list in single line.

        List<string> numbersInput = new List<string>();

        Console.WriteLine("Please enter an integer: ");
        string input1 = Console.ReadLine();

        while (!string.IsNullOrEmpty(input1))
        {
            numbersInput.Add(input1);
            Console.WriteLine("Please enter another integer: ");
            input1 = Console.ReadLine();
        }

        if (numbersInput.Count > 0)
        {
            Console.WriteLine("You have entered " + numbersInput.Count + " numbers, they were: ");
            var a = 1;
            foreach (var input in numbersInput)
            {
                Console.WriteLine("Number " + a++ + " = \t" + input);
            }
        }
        else
        {
            Console.WriteLine("You have entered 0 numbers.");
        }
Ajay
  • 1
  • 2
    Look at `Console.Write` since that doesn't make a new line every time. – DavidG Mar 11 '22 at 09:18
  • 1
    Try, instead of that foreach loop you have there, `Console.WriteLine("Numbers: " + string.Join(", ", numbersInput));` – Lasse V. Karlsen Mar 11 '22 at 09:19
  • 2
    I’m voting to close this question with its _["no actual problem to be solved"](https://stackoverflow.com/help/dont-ask)_, kinda sounds like _code improvement_ and/or _review_ and if so may be off-topic for SO. It _may_ be better suited for another SE site but be sure to read the relevant FAQ; and/or re-wording your question as necessary before cross-posting. [ask]. Good luck! –  Mar 11 '22 at 09:29
  • Using Linq, instead of writing `numbersInput.Count > 0` you can write `numbersInput.Any()` – LordWilmore Mar 11 '22 at 09:30
  • Also _"best/better"_ might be _subject to opinion_ and if so is sadly off-topic for SO. [ask]. Consider re-phrasing your question to be [constructive subjective](https://stackoverflow.com/help/dont-ask) –  Mar 11 '22 at 09:31
  • Console.WriteLine("Please enter an integer: "); string line; while ((line = Console.ReadLine()) != null) { numbersInput.Add(line); Console.WriteLine("Please enter another integer: "); } – VietDD Mar 11 '22 at 09:44
  • @MickyD it's definitely partially code improvement/review but there was also the question of "how to get console text to output onto a single line" - not the best question and almost certainly answered before; but not entirely off-topic for SO – Andrew Corrigan Mar 11 '22 at 10:28
  • 1
    Whilst the question's _body_ did report a bug, it was buried and not mentioned in the title. As mentioned it probably would have been better if the OP rephrased their question because the title on its own is not [constructive subjective](https://stackoverflow.com/help/dont-ask). Because the OP's question wasn't _constructive subjective_ whereby _["...insist that opinion be backed up with facts and references..."](https://stackoverflow.com/help/dont-ask)_ inevitably leads to people posting answers with unjustified conclusions or unproven claims. –  Mar 11 '22 at 10:52
  • Ajay, consider posting your question again but focusing on the fact that your current code is outputting to **multiple lines** but _you want a single-line output_. Best not to mention _"better"_ and _"optimized"_ –  Mar 11 '22 at 11:05

1 Answers1

0

You've got a few options:

Console.WriteLine() outputs whatever string you give it and starts a new line of text after it - akin to when we type and press the enter key.

Console.Write() outputs whatever string you give it - no more, and no less.

So you could simply replace the WriteLine in the loop with a Write...

Or you could build the entire string and pass it to a WriteLine(), something like:

List<string> numbersInput = new List<string>();

Console.WriteLine("Please enter an integer: ");
string input1 = Console.ReadLine();

while (!string.IsNullOrEmpty(input1))
{
   numbersInput.Add(input1);
   Console.WriteLine("Please enter another integer: ");
   input1 = Console.ReadLine();
}

if (numbersInput.Count > 0)
{
   Console.WriteLine("You have entered " + numbersInput.Count + " numbers, they were: ");
   var a = 1;
   var s = "";
   foreach (var input in numbersInput)
   {
      s = $"{s}, Number {a++} = \t{input}"; //string interpolation only applicable post C# 7
   }
   Console.WriteLine(s.Trim(", "));
}
else
{
   Console.WriteLine("You have entered 0 numbers.");
}

In the comments, Lasse suggests Console.WriteLine("Numbers: " + string.Join(", ", numbersInput)); (instead of the foreach loop) which would potentially be more efficient... but would have a different output format to your original question.

Alternatively, if you want to maintain the output format of your question, you could rejig things to use a Dictionary instead of a List. For instance:

Dictionary<string, string> numbersInput = new Dictionary<string, string>(); 

Console.WriteLine("Please enter an integer: ");
string input1 = Console.ReadLine();

var x = 1;
while (!string.IsNullOrEmpty(input1))
{
   //numbersInput.Add(input1);
   numbersInput.Add(input1, $"Number {x} = \t{input1}");
   x++;
   Console.WriteLine("Please enter another integer: ");
   input1 = Console.ReadLine();
}

if (numbersInput.Count > 0)
{
   Console.WriteLine("You have entered " + numbersInput.Count + " numbers, they were: ");
   Console.WriteLine(string.Join(", ", numbersInput.Values));
}
else
{
   Console.WriteLine("You have entered 0 numbers.");
}
Andrew Corrigan
  • 1,017
  • 6
  • 23
  • Shoot, you caught me - brain overtook my hands; was supposed to be "which would potentially be more efficient". I'll just edit that in – Andrew Corrigan Mar 11 '22 at 11:05
  • 1
    Some interesting stuff at [String.Join vs. StringBuilder: which is faster?](https://stackoverflow.com/questions/585860/string-join-vs-stringbuilder-which-is-faster) by the two titans Mr Skeet and Mr Gravell. Anyway, don't want to be a nag so here's +1 :) –  Mar 11 '22 at 11:26
  • 1
    Yeah, if it was the weekend I'd go down the rabbithole but this a "I need a break from work work" answer haha – Andrew Corrigan Mar 11 '22 at 11:28