Hi,everyone.
The problem is when I input "m" or "p" or other single input, it gives "ArgumentOutOfRangeException" in the line "int i = Int32.Parse(command.Substring(1, 1));". "if(command.Substring(0, 1) == "r" && i<= numCols)" these kind of condition use when the input like "r1,r2,r3...", then the following method takes number part as parameter to execute codes since i converted them already.
I was trying to set up length condition(command.Lenght>2) before "int i" part, but that does not work as then "i" can not be taken as an parameter into those methods. How can I fix this???
Any suggestion would be great!!! Thanks!!!

Console.Write("Enter command =>");
                string command = Console.ReadLine();              
                int i = Int32.Parse(command.Substring(1, 1));               
                
                if(command == "m") menu(); 
                else if (command == "p") PrintPuzzle(); 
                else if (command.Substring(0, 1) == "n") shuffle(i);
                else if (command.Substring(0, 1) == "r" && i<= numCols) moveRowRight(i);
                else if (command.Substring(0, 1) == "c" && i <= numRows) moveColDown(i);
                else if (command.Substring(0, 1) == "x")
                    System.Environment.Exit(0);
                else     
                    Console.WriteLine("Invalid command entered.");

Substring is a method that has two parametes:
1. Starting index
2. Numbers of characters

So when you call Substring(1, 1) -> that means you are taking 2nd character only (starting index is 1, and you take 1 character).

And because you are parsing to integer - it MUST be an integer. If there anythng else then a number it will give an error.

So a better handling of this kind of an exception, would be to check which character this is, and then decide what to do, like:

Console.Write("Enter command =>");
string command = Console.ReadLine();              
int i = 0;
if(int.TryParse(command.Substring(1, 1), out i)) //do the checking is characte is really a number 
{                
     if(command == "m") menu(); 
     else if (command == "p") PrintPuzzle(); 
     else if (command.Substring(0, 1) == "n") shuffle(i);
     else if (command.Substring(0, 1) == "r" && i<= numCols) moveRowRight(i);
     else if (command.Substring(0, 1) == "c" && i <= numRows) moveColDown(i);
     else if (command.Substring(0, 1) == "x")
           System.Environment.Exit(0);
     else     
           Console.WriteLine("Invalid command entered.");
}
else
       Console.WriteLine("Character is not a number - cannot continue.");

Maybe I didn't describe my problem well. While the input like "r2", it convert the 2nd character into int type then pass into the method like moveColRight(2). The part i'm struggling is "command.Substring(1, 1)" since sometimes the input only contains single character which causes "ArgumentOutOfRangeException".

Console.Write("Enter command =>");
String command = Console.ReadLine();
int i = 0;
if (command.Length > 1) {
    Int32.TryParse(command.Substring(1,1), out i);
}

switch (command[0]) {
    case 'm': menu(); break;
    case 'p': PrintPuzzle(); break;
    case 'n': shuffle(i); break;
    case 'r': if (i <= numCols) moveRowRight(i); break;
    case 'c': if (i <= numRows) moveColDown(i); break;
    case 'x': System.Environment.Exit(0); break;
    default: Console.WriteLine("Invalid command entered."; break;
}

Then you can try to do:

Console.Write("Enter command =>");
String command = Console.ReadLine();
int i = 0;
if (command.Length ==1) 
    Int32.TryParse(command.Substring(0,1), out i); //1 character input
else if(command.Lenght == 2)
    Int32.TryParse(command.Substring(1,1), out i); //2 characters input
//or more if you need

In case if you only want to get last character (if there is only character, it will take 1), then you can do:

Console.Write("Enter command =>");
string command = Console.ReadLine();
int i = int.Parse(command.Substring(command.Lenght -1, 1)); //this will always return last character - and oyu make sure its an integer!!

If the number is always the last character and is only a single digit you could do something like this

int i = 0;

if (int.TryParse(command.Last().ToString(), out i))
{

}

If there's a chance that command might be empty then you'd want to check for that before trying to call int.TryParse on it

You could also use command.First() to get the char value of the first character in the string. You'll need to add a "using System.Linq" for these methods if you don't already have it.

commented: Not Bad! +14
Console.Write("Enter command =>");
String command = Console.ReadLine();
int i = 0;
if (command.Length > 1) {
    Int32.TryParse(command.Substring(1,1), out i);
}

switch (command[0]) {
    case 'm': menu(); break;
    case 'p': PrintPuzzle(); break;
    case 'n': shuffle(i); break;
    case 'r': if (i <= numCols) moveRowRight(i); break;
    case 'c': if (i <= numRows) moveColDown(i); break;
    case 'x': System.Environment.Exit(0); break;
    default: Console.WriteLine("Invalid command entered."; break;
}

Thank you very much!!! IT WORKS NOW!!! One question what does command[0] mean???

string command = "xyz";
            char x = command[0];
            //char y = command[4]; compiles but blows up because index out of range.
            //string y = (string) command[0]; won't work. You can't implicitly or explicitly convert char to string
            //char[] y = (char[]) command; won't work. You can't implicitly or explicitly convert string to char[]

Think of a string as an implicit array of single characters, but not quite. It isn't a two way street or even a direct one way street. If you have intellisense it will tell you exactly what it can do as soon as you type "[" in the command. The commented code won't work and tells why as well

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.