Hi I have created a program with validation rules however on the option screen where userChoice is displayed the program crashes if you type a letter instead of a number and press enter. is there a validation rule that could be put in place for only a number to be entered instead of a letter. I already have a validation rule for numbers between 1 to 3 to be entered.
int userChoice, number;
static void Main(string[] args)
{
new Program().Welcome();
}
public void Welcome()
{
Console.WriteLine(" HELLO");
Console.ReadLine();
Main_Menu();
}
private void Main_Menu()
{
Console.WriteLine("1). Welcome");
Console.WriteLine("2). Help Facilities");
Console.WriteLine("3). Exit");
string userChoiceSTR = Console.ReadLine();
if (!string.IsNullOrEmpty(userChoiceSTR))
{
userChoice = Convert.ToInt16(userChoiceSTR);
try
{
Options();
}
catch
{
Console.WriteLine("Did not put any value. Please Select a menu: ");
Main_Menu();
}
}
else
{
Console.WriteLine("Did not put any value. Please Select a menu: ");
Main_Menu();
}
if (userChoiceSTR != "1" && userChoiceSTR != "2" && userChoiceSTR != "3")
{
Console.WriteLine("You need to provide a value between 1 and 3");
Main_Menu();
}
}
private void Options()
{
if (userChoice == 1)
{
Console.Clear();
Console.WriteLine("Welcome.....................");
Console.ReadLine();
}
if (userChoice == 2)
{
Console.Clear();
Console.WriteLine("Help.........................");
Console.ReadLine();
}
if (userChoice == 3)
{
//if user selects option 3 the program will exit
}