1

My output is a string of integers, displayed consecutively on the same line. Preferably using backslash n, how can I make them be displayed, each on a new line? This is what I have this far:

string even = "";

        while (true)
        {
            string inputData = Console.ReadLine();
            if (inputData.Equals("x", StringComparison.OrdinalIgnoreCase))
            {
                break;
            }

            for (int i = 0; i < inputData.Length; i++)
            {
                if (inputData[i] % 2 == 0)
                {
                     even +=inputData[i];

                }
            }

        }
        foreach (var e in even)
            Console.WriteLine(e);
        bool something = string.IsNullOrEmpty(even);
        if( something == true)
        {
            Console.WriteLine("N/A");
        }
        Console.ReadLine();

So, string even, that contains all the even numbers from input should be seen on the console on different lines. Any help would be appreciated!

Anna.A
  • 35
  • 4
  • 1
    Welcome to StackOverflow. Please share your current code and also what you have tried. Do you have your ints in a collection of some sort? – Gilad Green Oct 07 '18 at 07:36
  • 1
    Welcome to stackoverflow. Please take a minute to take the [tour], especially How to [Ask], and [edit] your question accordingly. As it is now, this question can't be answered other then by guessing – Zohar Peled Oct 07 '18 at 07:39
  • Welcome, Displaying on where place ? Console ? – Siavash Oct 07 '18 at 07:45
  • my bad. I've edited my question. – Anna.A Oct 07 '18 at 07:56

2 Answers2

0

Simple console otuput:

foreach (var e in even)
    Console.WriteLine(e);

Html output

var result = "";

foreach (var e in even)
    result += e + "<br>\n";

return Content(result);
Saeed Prez
  • 728
  • 3
  • 8
  • @GiladGreen I believe the main problem was mentioned, that a string of numbers needed to be split/displayed in separate lines. – Saeed Prez Oct 07 '18 at 08:02
0

Use string.Join to create one string out of your numbers string (a string is a collection of characters so you can do so), and specify the separator as Environment.NewLine:

var result = string.Join(Environment.NewLine, even.AsEnumerable());

As a side note, it is not advised to concat a string like that in a loop. Use StringBuilder. For explanations why see When to use StringBuilder? with the many linked questions.


Also worth mentioning that though inputData[i] % 2 == 0 works for you you should remember that iputData[i] is a character and not an integer. Therefore the % operates on the ASCII representation of the character. For the cases of numbers it works as expected but for instance also A % 2 == 0 will work.

And if familiar with linq you can refactor is as follows:

var even = string.Join(Environment.NewLine, inputData.Select(c => c % 2 == 0));
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • You may need to convert inputData into Enumerable – ManishM Oct 07 '18 at 08:28
  • @ManishM - in the first snippet I'm doing so and in the second there is no need :) – Gilad Green Oct 07 '18 at 08:29
  • Thank you, this has been really helpful! – Anna.A Oct 07 '18 at 08:37
  • @Anna.A - You are welcome :) did this help you solve the question? – Gilad Green Oct 07 '18 at 08:40
  • murphy's law. It worked until I found out that no solutions with linq are accepted. I ended up using another method, but now it doesn't work with multiple-digit numbers :( – Anna.A Oct 07 '18 at 08:57
  • @Anna.A - true but that is due to the input format. If you are just getting digits how can you tell if they are the same number or different numbers :) – Gilad Green Oct 07 '18 at 09:00
  • if my input is 12 7 5, the output will be 2. so 12 is treated as 1 and 2. I realize, the problem could be obvious, but I'm only just starting to learn, and I can't figure out what I'm doing wrong – Anna.A Oct 07 '18 at 09:12
  • @Anna.A - unfortunately this is a followup question and I shouldn't just answer it here. As a hint have a look at `Split` – Gilad Green Oct 07 '18 at 09:13