what i want to do is that when people using my program hit enter without anything in there it does not cause an error here is part of the program:
Console.WriteLine("4.-Ya no quiero jugar >.<");
int opc = Convert.ToInt16(Console.ReadLine());
switch (opc)
{
case 1:
Console.WriteLine("omg");
break;
case 2:
Console.WriteLine("wtf");
break;
default:
Console.WriteLine("Cant do that >.>");
Console.ReadKey();
break;
etc.
}
the thing is im using integers,i tried to do this
string opc1=console.readline();
if (opc =="")
{
console.writeline("nope,try again");
}
else
{ // Brace was omitted in original - Jon
int opc = Convert.ToInt16(Console.ReadLine());
switch (opc)
blah blah.
and different combinations of it >.< and default does not work for that
i hope some one can help me solve it >.<
First, Set your
Console.ReadLine()to a variable.Then check to see if the variable you set is not empty or null. Also, I’d recommend using the TryParse method of the Int16 class because it returns true or false depending on if the conversion was successful.
Also, you don’t need to convert your
ReadLineto an integer, because you can switch on Strings also. SinceReadLineis already aString, no conversion is necessary. However, if you need integers, try this: