There are a few things you can do. I'd first start off by re-reading the notes left above. And I am not suggesting you use either approach, I am just demonstrating some ways you can check the user input. You should use some get setters, but if you are looking to grade something, you are less likely to grade it by double values, and integers is what you would and should use. I can surely understand a use for doubles when averaging. Anyway, the get setters :
public static double doubNota { get; set; }
public static double doubSoma { get; set; }
public static int iNota { get; set; }
public static int iSoma { get; set; }
Using some regex, with a specific pattern, you could also use patterns for both integers, and doubles. A pattern for doubles would look like this :
Regex doubRegex = new Regex(@"^-?(?:0|[1-9][0-9]*)\.?[0-9]+$"); /* this pattern checks only numbers and . dot */
Where as a pattern for numbers would look like this (This could be expressed a little better but I'm tight on time, and It will give you something to play with) :
Regex intRegex = new Regex(@"^[0-9]+$"); /* this pattern checks only numbers */
Set the value variable to be the line you want to evaluate.
var value = Console.ReadLine();
You would then check the pattern for a match and do as you please with the double value :
if (doubRegex.IsMatch(value)) /* Our double is not a string, so we convert to string to compare the pattern - IsMatch takes a string */
I suppose if a double value is not matched by the pattern, you could run an else if statement for the intRegex like so :
else if (intRegex.IsMatch(value))
And perhaps an else statement for non matching conditions above :
else
{
Console.WriteLine("That is not an accepted number");
}
You could also use some static functions to evaluate whether or not the input value is actually a double or not by executing something like the below functions which will also update your get setters with the converted result if succeeded :
public static bool IsInputDoubles(string value)
{
var newValue = 0.0;
if (double.TryParse(value, out newValue)) /* Is it a double */
{
doubNota = newValue;
return true;
}
return false;
}
public static bool IsInputIntegeral(string value)
{
var newValue = 0;
if (int.TryParse(value, out newValue)) /* Is it a integer */
{
iNota = newValue;
return true;
}
return false;
}
You would then call these functions with the below code and if they return true. the conversions succeeded, and if they are false, add an else condition for failures and evaluation? As follows :
if (IsInputDoubles(value)) /* Is it a double? */
{
Console.WriteLine($"Double is {doubNota}");
}
AND :
if (IsInputIntegeral(value)) /* Is it a integer */
{
Console.WriteLine($"Integer is {iNota}");
}
Now it's up to you to put it together. I've tested the regular expression patterns and they appear to work after running one or two quick tests. And my preferences are to use TryParse over Parse, because if the input value is not in the expected format, and if it can't be parsed using
Parse
, you will be likely introduced to a FormatException or alike. Whereas
TryParse
returns a bool value indicating if the conversion succeeded or not, and this thus not throw an exception, and it is my preferred choice since it fails silently. Remember to check the condition of the result. I would only use Parse if I was 100% sure that the validation events that took place prior executing
Parse
ensured the value was correct and evaluated before executing it with Parse method.
Just to elaborate on one other point. Note I am using property values of
iSoma
, and
iNota
. These are only for type integral values; hence the
i
before the name. The same for
doubNota
and
doubSoma
, the
doub
before your variable name implying it should be used with doubles. And so, if you wish to work with additional values, you should assign additional individual properties specific to their variable type. Further note; when user input has been validated and parsed, you will refer to these properties directly when you want to update or use the values results.
How I allow for non-numeric input too?
That depends on what user-input is received. Typically using simple conditional if-else statements can suffice.
This post was longer than i wanted it to be, but hopefully helps.