Welcome to Dream.In.Code
Become a C# Expert!

Join 149,942 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,419 people online right now. Registration is fast and FREE... Join Now!




Index was outside the bounds of the array

2 Pages V  1 2 >  
Reply to this topicStart new topic

Index was outside the bounds of the array, console application

klaibert26
29 Jan, 2008 - 06:06 AM
Post #1

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 5

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;
                }
            }
        }
    }
}

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Index Was Outside The Bounds Of The Array
29 Jan, 2008 - 06:27 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,156



Thanked: 44 times
Dream Kudos: 125
My Contributions
I am not a C# expert but I think that

wordlist.length will not give you the number of words in a string. I think it will give you the length of string placed at first in array wordlist.

so that string might be larger than your wordlist array size and you would be exceeding your array limit because of that.

say it had only 3 words but first word had 6 letters, so iteration count will be 6 and it will give this error for 4th iteration.

I am not sure of this but still you can think on this.
User is offlineProfile CardPM
+Quote Post

klaibert26
RE: Index Was Outside The Bounds Of The Array
29 Jan, 2008 - 06:50 AM
Post #3

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 5

QUOTE(AmitTheInfinity @ 29 Jan, 2008 - 07:27 AM) *

I am not a C# expert but I think that

wordlist.length will not give you the number of words in a string. I think it will give you the length of string placed at first in array wordlist.

so that string might be larger than your wordlist array size and you would be exceeding your array limit because of that.

say it had only 3 words but first word had 6 letters, so iteration count will be 6 and it will give this error for 4th iteration.

I am not sure of this but still you can think on this.



hi;

thank's for reply so fast.

i disagree width you.
the wordlist.length is giving me the number of words.

the output:

The program will analyse sentences you type in.

Please enter a sentence: firststring secondstring thirdstrinf

The number of words in the sentence is 3




User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Index Was Outside The Bounds Of The Array
29 Jan, 2008 - 07:06 AM
Post #4

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
Usually when you work with arrays, the Length function returns the actual length of the array, as opposed to the zero based index that we're used to working with. So I would bet that if you were to walk through your code, your for loop would be coming up with this error on the last loop through that for loop. Try replacing wordList.length with wordList.length - 1

CODE

                for (i = 0; i <= wordList.Length - 1; i++)


Hope this helps!
User is offlineProfile CardPM
+Quote Post

klaibert26
RE: Index Was Outside The Bounds Of The Array
29 Jan, 2008 - 07:31 AM
Post #5

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 5

QUOTE(Sothrie @ 29 Jan, 2008 - 08:06 AM) *

Usually when you work with arrays, the Length function returns the actual length of the array, as opposed to the zero based index that we're used to working with. So I would bet that if you were to walk through your code, your for loop would be coming up with this error on the last loop through that for loop. Try replacing wordList.length with wordList.length - 1

CODE

                for (i = 0; i <= wordList.Length - 1; i++)


Hope this helps!


Hi Sothrie'

Thank's very much. icon_up.gif
it's solve almost averything.

one last problem...

when he ask : "Do you want to analyse another sentence (Y/N)? "

when i click "y" nothing happen
do u know why?




neve mind.
i solved it

thank u for all the help rolleyes.gif

here is the code that works fine:

CODE

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

// This program is Assignment 4 for Software Development 1 2006/2007.
//
// The program prompts the user to enter sentences. It will then analyse
// them to give a total word count, the number of non-white space characters,
// plus a count of how many words there are of length 1, 2, 3, 4, 5 and 6 or greater.

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++)//correction: expected ";"
                {
                    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 - 1));// wordList.Length - 1 instead wordList.Length
                for (i = 0; i <= wordList.Length - 1; i++)// wordList.Length - 1 instead wordList.Length
                {
                    word = wordList[i].Trim();// correction:
                    if (word.Length > 0)
                    {
                        if (word.Length > 5)
                        {
                            sizeCount[0]++;
                        }
                        else
                        {
                            sizeCount[word.Length]++;// correction: "Length" and not "length"
                        }
                        characterCount = characterCount + word.Length;
                    }
                }
                Console.WriteLine("The number of words with 1 letter is " + sizeCount[1]);
                for (i = 2; i <= 5; i++)// for i=2 insted i=1 becase 1 letter we alredy checked
                {
                    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 == "N") // "N" insted of "Y"
                {
                    continueRunning = false;
                }
            }
        }
    }
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Index Was Outside The Bounds Of The Array
29 Jan, 2008 - 10:06 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Use code.gif tags when posting code.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Index Was Outside The Bounds Of The Array
29 Jan, 2008 - 10:39 AM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,281



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Just a quick note, always prefer for (i = 0; i < wordList.Length; i++) to for (i = 0; i <= wordList.Length - 1; i++). The reasoning here is that wordList.Length - 1 requires an extra, unnecessary, operation. There's a reason arrays have zero based indexes and that's one of them.

On the flip side, for debugging and also safety, using the most limited scope of that loop variable is also prefered. e.g. for (int i = 0; i < wordList.Length; i++). Here, i exists only for the scope of the loop. You can't accidently mess with it elsewhere in the program.

Also, in you program, this logic is off with the wordList.Length-1. The output of this:
CODE

string sentence = "This is a test";
string [] wordList = sentence.Split(new char[] { ' ' });
Console.WriteLine(wordList.Length);


Is 4.

Hope this helps.

User is online!Profile CardPM
+Quote Post

brightcomit
RE: Index Was Outside The Bounds Of The Array
10 Sep, 2008 - 07:04 AM
Post #8

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 7


My Contributions
hi,
im having the same problem..can anybody sort it out?

CODE

public void parse(string path)
        {
            char[] t =new char[5];
            //path = strFileNameForm1;
            int len = path.Length;
            char[] temp = path.ToCharArray();
            for (int i = len-1; i >= 0; i--)
                {
                    if (temp[i] == '.')
                    {
                        t[i] = temp[i]; // the problem is here
                        i--;
                    }
                    if (temp[i] == '\\')
                        break;                    
                }            
            temp2 = t.ToString();                
            }
        }

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Index Was Outside The Bounds Of The Array
10 Sep, 2008 - 07:45 AM
Post #9

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
What if path is greater than 5 characters in length? Then i could be 10, which is outside the bounds of the t array.
User is offlineProfile CardPM
+Quote Post

brightcomit
RE: Index Was Outside The Bounds Of The Array
13 Sep, 2008 - 01:28 AM
Post #10

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 7


My Contributions
ya...the length of path is 30..
but im extracting only 5 characters still it wud be out of bound??

and i have tried to set size of t[30]...still it gives the same error... sad3.gif

This post has been edited by brightcomit: 13 Sep, 2008 - 01:32 AM
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Index Was Outside The Bounds Of The Array
13 Sep, 2008 - 07:04 AM
Post #11

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
What exactly are you trying to do here? There may be an easier way.
User is offlineProfile CardPM
+Quote Post

brightcomit
RE: Index Was Outside The Bounds Of The Array
15 Sep, 2008 - 12:57 AM
Post #12

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 7


My Contributions
actually im recording a wave file..
so i have to extract the name of wav file from the path,
like e.g., C:\\wav files\\one.wav
i have to extract one from the path...

is there any easier way?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month