I am basically trying to continue a loop until user enters the required input. I got it to work if less than 1 or greater than 7 is input. But I still get an "Unhandled Exception" error if user inputs either space characters or just hits ENTER. Any help or suggestion is greatly appreciated.
Here is the code-
static void Main(string[] args)
{
string[] daysOfWeek = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
System.Console.WriteLine(" Which day would you like?");
System.Console.Write(" Choose from 1 and 7. ");
int? iDay = int.Parse(Console.ReadLine());
// Loops until user enters number from 1 - 7
while ( iDay < 1 || iDay > 7 || iDay is null )
{
if ( iDay > 0 && iDay < 8)
{
break;
}
System.Console.Write(" < INVALID - CHOOSE FROM 1 AND 7 > ");
iDay = int.Parse(Console.ReadLine());
}
string chosenDay = daysOfWeek[(int)iDay-1];
System.Console.WriteLine($"You chose {chosenDay}.");
int.TryParseinstead ofint.Parse... Int32.TryParse Method