Repeating the question if user input is empty in C#

Viewed 663

I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.

System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();

System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();

System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();
5 Answers

You could try

string firstName = null;
Console.WriteLine("Give first name");
while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
  Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");

You can do the same for DateTime

Console.Write("Give date of birth");
while (!DateTime.TryParse(Console.ReadLine(),out var dob))
    Console.Write("OMG you had one job as a user of this application, to put in the right value!");

Additional Resources

DateTime.TryParse Method

Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.

you can write

   System.Console.Write("Give first name");
   String firstname = null;
   while(firstname==null || firstname==""){
       firstname = System.Console.ReadLine();
   }

Try this:

String lastname = null;

Console.Write("Give last name: ");

while (!String.IsNullOrEmpty(lastname)) {
    lastname = System.Console.ReadLine();
}

Try this

            var firstname = string.Empty;
            var lastname = string.Empty;
            var dt = DateTime.MinValue;
            do
            {
                System.Console.Write("Give first name");
                 firstname = System.Console.ReadLine();

            } while (string.IsNullOrEmpty(firstname));
            do
            {
                System.Console.Write("Give last name");
                 lastname = System.Console.ReadLine();
            } while (string.IsNullOrEmpty(lastname));


            do
            {
                System.Console.Write("Give date of birth");
                 dt = DateTime.Parse(System.Console.ReadLine());
            } while (dt != DateTime.MinValue);

try this

        bool value = true;

        while (value==true)
        {
            System.Console.Write("Give first name");
            String firstname = System.Console.ReadLine();
            if (firstname == "")
            {
                value = false;
                break;
            }
            System.Console.Write("Give last name");
            String lastname = System.Console.ReadLine();
            if (lastname == "")
            {
                value = false;
                break;
            }
            System.Console.Write("Give date of birth");
             DateTime dt = DateTime.Parse(System.Console.ReadLine());
            if (dt.ToString()=="")
            {
                value = false;
                break;
            }
        }
Related