Index was outside the bounds of the array

console application

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 77509 Views - Last Post: 11 April 2010 - 06:08 AM Rate Topic: -----

#1 klaibert26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-January 08

Index was outside the bounds of the array

Posted 29 January 2008 - 07:06 AM

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 "

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



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


Is This A Good Question/Topic? 0
  • +

Replies To: Index was outside the bounds of the array

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Index was outside the bounds of the array

Posted 29 January 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.
Was This Post Helpful? -3
  • +
  • -

#3 klaibert26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-January 08

Re: Index was outside the bounds of the array

Posted 29 January 2008 - 07:50 AM

View PostAmitTheInfinity, on 29 Jan, 2008 - 07:27 AM, said:

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

Was This Post Helpful? 0
  • +
  • -

#4 Sothrie  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 03-December 07

Re: Index was outside the bounds of the array

Posted 29 January 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

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



Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#5 klaibert26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-January 08

Re: Index was outside the bounds of the array

Posted 29 January 2008 - 08:31 AM

View PostSothrie, on 29 Jan, 2008 - 08:06 AM, said:

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

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



Hope this helps!


Hi Sothrie'

Thank's very much. :^:
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:

here is the code that works fine:

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

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Index was outside the bounds of the array

Posted 29 January 2008 - 11:06 AM

Use :code: tags when posting code.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: Index was outside the bounds of the array

Posted 29 January 2008 - 11:39 AM

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:
string sentence = "This is a test";
string [] wordList = sentence.Split(new char[] { ' ' });
Console.WriteLine(wordList.Length);



Is 4.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 brightcomit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-February 08

Re: Index was outside the bounds of the array

Posted 10 September 2008 - 08:04 AM

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

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


Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Index was outside the bounds of the array

Posted 10 September 2008 - 08:45 AM

What if path is greater than 5 characters in length? Then i could be 10, which is outside the bounds of the t array.
Was This Post Helpful? 0
  • +
  • -

#10 brightcomit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-February 08

Re: Index was outside the bounds of the array

Posted 13 September 2008 - 02:28 AM

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:

This post has been edited by brightcomit: 13 September 2008 - 02:32 AM

Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Index was outside the bounds of the array

Posted 13 September 2008 - 08:04 AM

What exactly are you trying to do here? There may be an easier way.
Was This Post Helpful? 0
  • +
  • -

#12 brightcomit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-February 08

Re: Index was outside the bounds of the array

Posted 15 September 2008 - 01:57 AM

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?
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: Index was outside the bounds of the array

Posted 15 September 2008 - 03:45 AM

Wait, that code was C#!?! Why the hell are you doing this: char[] t =new char[5];?

You have strings, use them! They're dynamic and are your friends.

Try this:
string path = "C:\\wav files\\one.wav";
string fileName = path.Substring(path.LastIndexOf('\\')+1);
string fileNameSansExt = fileName.Substring(0,fileName.LastIndexOf('.'));



Or, now that you know strings will do the work for you, just do this, it's safer anyway:
string path = "C:\\wav files\\one.wav";
string fileNameSansExt = System.IO.Path.GetFileNameWithoutExtension(path);


Was This Post Helpful? 2
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Index was outside the bounds of the array

Posted 15 September 2008 - 04:20 AM

Yep, that's what I was going to say.
Was This Post Helpful? 0
  • +
  • -

#15 brightcomit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-February 08

Re: Index was outside the bounds of the array

Posted 15 September 2008 - 10:15 AM

i did it.. :bananaman: :D

public void parse(string path)
		{
			string temp2;
			int len = path.Length;
			char[] t =new char[len];
			char[] temp = path.ToCharArray();
			for (int i = len-1; i >= 0;)
			{
				if (temp[i] != '\\')
				{
					t[i] = temp[i];
					i--;
				}
				else if (temp[i] == '\\')
					break;
			}
			temp2 = new string(t);
			temp2 = temp2.Replace("\0", "");
			int l = temp2.Length;
			WavId = temp2.Substring(0, l-4);
		}


may be it wud be of someone's help so i posted it here :)

actually im very poor in programming..gradually i'll learn to lessen my code..

im on tricycle..after some coding practice will jump to bicycle ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2