tags:

views:

298

answers:

6

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 >.<

A: 

you might look into using a try catch statement for error handling...

tbischel
+10  A: 

Check the Int16.TryParse method.

This will allow you to exit from the program or perform another action if the user input is not a number in the range allowed by Int16 (negative 32768 through positive 32767).

Sample code can be found on MSDN entry (Int16.TryParse Method).

João Angelo
Thank you im going to try it right now
Makenshi
wooo thank you very much every1 try parse rox!! XD
Makenshi
+1  A: 

I think what you want is int.Parse(...)

TheSean
+4  A: 

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 ReadLine to an integer, because you can switch on Strings also. Since ReadLine is already a String, no conversion is necessary. However, if you need integers, try this:

String lineIn = Console.ReadLine();

if (!String.IsNullOrEmpty(lineIn))
{
    Int16 myNum;
    if (Int16.TryParse(lineIn , out myNum))
    {
            switch(myNum)
            {
                    case 1:
                    ...
                    default:
                    ...
            }
    }
}
Michael G
thank you im going to try it right now too xD
Makenshi
Technically you don't need any of the if statements in the example. If you only execute Int16.TryParse(lineIn, out myNum); then you will end up with the integer the user entered as the value of myNum or 0 if the input was null, empty, a string or a number outside the min/max value of Int16. The value of 0 would then be picked up by the default case.
Alexander Kahoun
A: 

TryParse:

string str;
short val;
while(!short.TryParse(str=Console.ReadLine(), out val))
{
    Console.WriteLine("Cant do that  >.>");
}
excelsior
Hi. If you want your code snippet to look pretty, try surrounding it in <pre><code> tags. <pre><code>my code</code></pre>. There is also a tool in the wysiwyg editor (the 101010 one). I only mention it because it took me a few tries to find it.
John Buchanan
That's more like it! :)
excelsior
A: 

For getting an integer i usually use a recursive function like this

private int GetInt()
{
     try
     {
         return int.parse(Console.Readline().Trim());
     } 
      catch (exception e) 
     {
         Console.WriteLine(string.format("{0} Please try again", e.message);
         return GetInt();
     }
}
Paul Creasey