Input Validations in C# at C Sharp for Beginners Course Codeasy.Net

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?

Hi ,

I think there is a way to make your code work, but it is overwhelmed with while loops and thus - logic. I ran it a couple of times and got a few “out of memory exceptions”. Let me instead suggest you a way to solve it in a simpler way.
You need only 1 while loop - you already have it, the outer one. Inside that loop, you first need to check if the first name is empty - if it is, output the corresponding error message to the screen. If the first name is not empty, in the else clause, you check if the length of the first name is less than 2. If it is, - output the error message. The same logic works for the last name. The lines that read the new values could be moved to the end of the while loop. This is much easier to read and understand.

while (...)
{
    if (firstName ...)
    ...
    else if ()
    ...

    if (lastName ...)
    ...
    else if ()
    ...

    // Read new first name
    // Read new last name
}

Worked out. Thanks a lot!

1 Like