mardi 1 décembre 2015

Index Out Of Range while comparing two array elements

This is a part of a bigger problem I'm trying to solve, but index out of range exception seems to be an invisible hurdle to me. This if loop works well in c++ but in c#, idk whats happening. Any help is appreciated. This is very important to me. Please find the root for this exception.

    namespace damn
{
class Program
{
    static void Main(string[] args)
    {

        string InputString = Console.ReadLine();
        string[] stringArray = InputString.Split(',');
        int[] intArray = new int[stringArray.Length];
        for (int i = 0; i < stringArray.Length; i++)
        {
            intArray[i] = int.Parse(stringArray[i]);
        }

        int length = stringArray.Length;

        for(int i=0; i<length; i++)
           {
             if (intArray[i] > intArray[i + 1])
                Console.WriteLine("ok");
            else
                Console.WriteLine("not ok");
           }

    }

IndexOutOfRangeException was unhandeled.But how? the logic is right, ain't it? Call me a noob or what ever, this is not the actual program, but a part of it.

input sample -

          1,2,3,4 [/enter/]

Aucun commentaire:

Enregistrer un commentaire