How to check if the user typed something when prompted for a C#entry

Asked

Viewed 445 times

1

The following validation checks whether an entry is a positive number.

if (Q[i] < 0) {
    Console.WriteLine("Digite um número positivo!");
    goto Start;
}

Just like this check, if nothing was typed, the program would just go back to "Start" without an error.

That is, how do I check if the entry is empty as the user has not typed anything?

  • Deal with a variable if the data entry is NULL or not. string text; if (text == NULL){ Console.Writeline("Nothing typed"); } Else{ Console.Writeline("Has been typed : ", text); ;}

1 answer

1

 Console.Write("Informe um valor: ");
 string value = Console.ReadLine();
 if (String.IsNullOrEmpty(value))
 {
     Console.WriteLine("O usuário não informou valor");
     goto Start;
 }
 else
 {
    Console.WriteLine("O valor é: {0}", s);
 }

Note that I used the boolean method String.Isnullorwhitespace, because it indicates whether a specified character string is null, empty, or consists of whitespace characters only. So if the variable has no character, it will enter the first if, stating that the user did not enter any value.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.