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

0

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 only return to the "Start" without displaying an error.

That is, how do I check if the entry is empty because the user did not enter anything?

    
asked by anonymous 23.11.2017 / 16:07

1 answer

0
 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 string is null, empty, or consists of only whitespace characters. So if the variable has no character, it will then enter the first if , stating that the user did not enter any value.

I hope I have helped.

    
24.11.2017 / 05:13