1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Index was outside the bounds of the array CONSOLE APPLICATION

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

  1. klaibert26

    klaibert26 New Member

    Joined:
    Jan 29, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    for (i = 0; i <= wordList.Length; i++)
    It should be <, not <=.
    <= will loop one too many times.
     
  3. klaibert26

    klaibert26 New Member

    Joined:
    Jan 29, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thank's :pleased:
     

Share This Page