I have this problem to solve in C#: the user enters integers from console (not a fixed number of int, but unlimited numbers of int, as much as he wants) one by one and when the user enters the value "x" the program should stop and should count and print how many even numbers the string contains.
Example
The user enters these integers from console:
2 5 8 4 1 6 9 2 5 x
and the result should be:
5
This is my code so far:
using System;
public class TestNumericDataTypesOperations
{
public static void Main()
{
string[] a = new string[100];
int b=0;
int total = 0;
for (int i = 0; i < a.Length; i++)
{
a[i] = Console.ReadLine();
int b = int.Parse(a);
if (b % 2 == 0)
total++;
if (a[i] == "x")
{
break;
}
Console.WriteLine(total);
}
}
}
I get error CS1503 and CS0136 on line 17 (int b = int.Parse(a);
) and warning CS0219 on line 10 (int b=0;
) and warning CS8601 on line 16 (a[i] = Console.ReadLine();
).
Thank you!
The first issue:
int.Parse(a);
a
here is an array, not a string. You need to use the specific entry in a
instead.
But there's a worse issue the compiler missed: you're doing this before checking whether the input is x
, meaning you're expecting to see non-integer values here at least once. That will cause an exception the compiler can't anticipate, when the program is running. You should look at int.TryParse()
instead, and move this part to after checking if the user wants to quit.
Then we have the warning for this line:
int b = 0;
The problem here is this variable is never used. Rather, inside the loop the variable is re-declared: int b = int.Parse(a);
. Either remove the int
from the later line, or (for preference) remove the first line entirely.
Finally we have the warning for this line:
a[i] = Console.ReadLine();
This relates to a new-ish C# feature for nullable reference checks. The Console.Readline()
method is much older than these checks, and for historical reasons doesn't mark its result as not null. You can either turn off these checks completely (not recommended) or add a special null-forgiveness operator:
a[i] = Console.ReadLine()!;
There is one final issue, on this line:
Console.WriteLine(total);
This line is still inside the loop, which means it will re-write the total on every entry. You probably want to do this only once, after the loop.
But for fun, since we don't ever use the original inputs again, I'd write it like this (no need for an array):
public static void Main()
{
int total = 0;
string input;
while( (input = Console.ReadLine()) != "x")
{
if (int.TryParse(input, out int b) && b % 2 == 0)
total++;
}
Console.WriteLine(total);
}
And this is useful to demonstrate how to call int.TryParse()
For more fun, and to give you exposure to some advanced language features, we could add this method (needs a using System.Collections.Generic;
at the top):
public static IEnumerable<int> ReadData()
{
string input;
while ( (input = Console.ReadLine()) != "x")
{
if (int.TryParse(input, out int i))
{
yield return i;
}
}
}
And then the actual program method is a one-liner (needs using System.Linq;
):
public static void Main()
{
Console.WriteLine(ReadData().Count(i => i%2 == 0));
}