Hello, I’m working on this task:
Write a program that reads a user’s first name and last name from the console input (each on its own line). Both names must not be empty and must consist of two characters or more. If a user inputs an empty string for the first name, you should output “First name can’t be empty! Try again.” Same for the last name: “Last name can’t be empty! Try again.” If the first name is only one character long, the program should output: “First name is too short! Try again.” and the same for last name: “Last name is too short! Try again.” The program should ask for and read the first and last name until the user inputs valid values for both. Then it should print “My greetings, {firstName} {lastName}!” For example:
>
>Walker
First name can’t be empty! Try again.
>Alan
>
Last name can’t be empty! Try again.
>
>
First name can’t be empty! Try again.
Last name can’t be empty! Try again.
>Alan
>W
Last name is too short! Try again.
>Alan
>Walker
My greetings, Alan Walker!
Here is my code:
namespace InputValidation
{
class IDontEvenKnowMyName
{
static void Main(string[] args)
{
var firstName = Console.ReadLine();
bool firstNameEmpty= string.IsNullOrEmpty(firstName);
int firstNameLength = firstName.Length;
var secondName = Console.ReadLine();
bool SecondNameEmpty = string.IsNullOrEmpty(secondName);
int SecondNameLength = secondName.Length;
while (firstNameEmpty || SecondNameEmpty || firstNameLength < 2 || SecondNameLength < 2)
{
if (firstNameEmpty)
Console.WriteLine("First name can't be empty! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
if (SecondNameEmpty)
Console.WriteLine("Last name can't be empty! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
if (firstNameLength < 2)
Console.WriteLine("First name is too short! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
if (SecondNameLength < 2 )
Console.WriteLine("Last name is too short! Try again.");
firstName = Console.ReadLine();
firstNameEmpty= string.IsNullOrEmpty(firstName);
firstNameLength = firstName.Length;
secondName = Console.ReadLine();
SecondNameEmpty = string.IsNullOrEmpty(secondName);
SecondNameLength = secondName.Length;
}
Console.WriteLine($"My greetings, {firstName} {secondName}!");
}
}
}
Can anyone please help me to solve it?