1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Index was outside the bounds of the array CONSOLE APPLICATION

Discussion in 'C#' started by klaibert26, Jan 29, 2008.

  1. klaibert26

    klaibert26 New Member

    Hi

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

    the error is in line:
    word = wordList.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;
                    }
                }
            }
        }
    }
     
    Last edited by a moderator: Jan 29, 2008
  2. oogabooga

    oogabooga New Member

    for (i = 0; i <= wordList.Length; i++)
    It should be <, not <=.
    <= will loop one too many times.
     
  3. klaibert26

    klaibert26 New Member

    Thank's :pleased:
     

Share This Page