1

My input is a string of integers, which I have to check whether they are even and display them on the console, if they are. The problem is that what I wrote checks only the individual digits and not the numbers.

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");
}

For example, if the input is: 12 34 56 my output is going to be 2 4 6 (every number needs to be displayed on a new line). What am I doing wrong? Any help is appreciated.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Anna.A
  • 35
  • 4

2 Answers2

2

Use string.Split to get the independent sections and then int.TryParse to check if it is a number (check Parse v. TryParse). Then take only even numbers:

var evenNumbers = new List<int>();
foreach(var s in inputData.Split(" ")) 
{
    if(int.TryParse(s, out var num) && num % 2 == 0)
        evenNumbers.Add(num); // If can't use collections: Console.WriteLine(num);
}

(notice the use of out vars introduced in C# 7.0)

If you can use linq then similar to this answer:

var evenNumbers = inputData.Split(" ")
                           .Select(s => (int.TryParse(s, out var value), value))
                           .Where(pair => pair.Item1)
                           .Select(pair => pair.value);
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • Unfortunately System.Collections.Generic isn't an option either. I did look up Split, but I couldn't make it work. – Anna.A Oct 07 '18 at 11:18
  • @Anna.A - In that case then instead of adding items to the list just print with `Console.WriteLine`. I personally prefer not to mix the "presentation" with the "filtering" but if those are your requirements.. – Gilad Green Oct 07 '18 at 11:20
  • @Anna.A - Then you should show what you have tried using `Split` – Gilad Green Oct 07 '18 at 11:20
  • First of all, I don't know what the delimiter is. Split by space worked when my input was number space number space...etc. The input however needs to be number enter number enter – Anna.A Oct 07 '18 at 11:30
  • @Anna.A - If you don't have knowledge of a delimiter then there is in any case not much you can do.. because how will you know if `1234` is one number two (and which) or 4..... – Gilad Green Oct 07 '18 at 11:36
  • ok, i'm onto something, now, it does display the entire number, only every digit on a new line. either that, or consecutively. Can I have your opinion? Is this a simple exercise? It was regarded as a simple exercise. – Anna.A Oct 07 '18 at 11:53
  • @Anna.A - It is still not clear what is the input format and what you are doing in your code. If it is as I'm doing above then it shouldn't be each digit separately – Gilad Green Oct 07 '18 at 11:57
1

I think you do too many things here at once. Instead of already checking if the number is even, it is better to solve one problem at a time.

First we can make substrings by splitting the string into "words". Net we convert every substring to an int, and finally we filter on even numbers, like:

var words = inputData.Split(' ');  # split the words by a space
var intwords = words.Select(int.Parse);  # convert these to ints
var evenwords = intwords.Where(x => x % 2 == 0);  # check if these are even
foreach(var even in evenwords) {  # print the even numbers
    Console.WriteLine(even);
}

Here it can still happen that some "words" are not integers, for example "12 foo 34". So you will need to implement some extra filtering between splitting and converting.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555