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!
using System;
namespace InputValidation
{
class IDontEvenKnowMyName
{
static void Main(string[] args)
{
var firstName = Console.ReadLine();
var secondName = Console.ReadLine();
while (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(secondName) || firstName.Length < 2 || secondName.Length < 2)
{
while (string.IsNullOrEmpty(firstName) && string.IsNullOrEmpty(secondName))
{
Console.WriteLine("First name can't be empty! Try again.");
Console.WriteLine("Last name can't be empty! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
}
while(string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(secondName))
{
Console.WriteLine("First name can't be empty! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
}
while(string.IsNullOrEmpty(secondName) && !string.IsNullOrEmpty(firstName))
{
Console.WriteLine("Last name can't be empty! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
}
while ((firstName.Length < 2 && secondName.Length < 2) && (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(secondName)))
{
Console.WriteLine("First name is too short! Try again.");
Console.WriteLine("Last name is too short! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
}
while (firstName.Length < 2 && !string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(secondName))
{
Console.WriteLine("First name is too short! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
}
while (secondName.Length < 2 && !string.IsNullOrEmpty(secondName) && !string.IsNullOrEmpty(firstName))
{
Console.WriteLine("Last name is too short! Try again.");
firstName = Console.ReadLine();
secondName = Console.ReadLine();
}
}
Console.WriteLine($"My greetings, {firstName} {secondName}!");
}
}
}
I have tested every possible combination. Everything should work as intended, but codeasy doesn’t accept it somehow. Any help?