Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Index was outside the bounds of the array CONSOLE APPLICATION (http://www.go4expert.com/forums/index-outside-bounds-array-console-t8541/)

klaibert26 29Jan2008 19:25

Index was outside the bounds of the array CONSOLE APPLICATION
 
Hi

i have a error in my program;
can anyone help?

the error is in line:
word = wordList[i].Trim();

"Index was outside the bounds of the array "

--------------------------------------------------------------

Code:


using System;
using System.Collections.Generic;
using System.Text;



namespace Assignment4
{
    class WordCount
    {
        static void Main(string[] args)
        {
            string sentence;
            string[] wordList;
            string word;
            int[] sizeCount = new int[6];
            int i;
            int characterCount = 0;
            string runAgain;
            bool continueRunning = true;

            Console.WriteLine("The program will analyse sentences you type in.");
            while (continueRunning == true)
            {
                for (i = 0; i <= 5; i++)
                {
                    sizeCount[i] = 0;
                }
                Console.WriteLine();
                Console.Write("Please enter a sentence: ");
                sentence = Console.ReadLine();
                while (sentence.Length == 0)
                {
                    Console.Write("You must enter some text. Please try again: ");
                    sentence = Console.ReadLine();
                }
                Console.WriteLine();
                wordList = sentence.Split(new char[] { ' ' });
                Console.WriteLine("The number of words in the sentence is " + wordList.Length);
                for (i = 0; i <= wordList.Length; i++)
                {
                    word = wordList[i].Trim();
                    if (word.Length > 0)
                    {
                        if (word.Length > 5)
                        {
                            sizeCount[0]++;
                        }
                        else
                        {
                            sizeCount[word.Length]++;
                        }
                        characterCount = characterCount + word.Length;
                    }
                }
                Console.WriteLine("The number of words with 1 letter is " + sizeCount[1]);
                for (i = 1; i <= 5; i++)
                {
                    Console.WriteLine("The number of words with " + i + " letters is " + sizeCount[i]);
                }
                Console.WriteLine("The number of words with 6 or more letters is " + sizeCount[0]);
                Console.WriteLine("The number of non-white space characters is " + characterCount);
                Console.WriteLine();
                Console.Write("Do you want to analyse another sentence (Y/N)? ");
                runAgain = Console.ReadLine().ToUpper();
                while (runAgain != "Y" && runAgain != "N")
                {
                    Console.Write("Please enter Y or N: ");
                    runAgain = Console.ReadLine().ToUpper();
                }
                if (runAgain == "Y")
                {
                    continueRunning = false;
                }
            }
        }
    }
}


oogabooga 30Jan2008 19:39

Re: Index was outside the bounds of the array CONSOLE APPLICATION
 
for (i = 0; i <= wordList.Length; i++)
It should be <, not <=.
<= will loop one too many times.

klaibert26 30Jan2008 19:42

Re: Index was outside the bounds of the array CONSOLE APPLICATION
 
Thank's :pleased:


All times are GMT +5.5. The time now is 14:32.