Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I keep getting the message "Index is out of bounds of the array," for the following code. Also, I cannot get the lblAverage to display the average. I just get NaN.

Code:

Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click
        'Determine word count and average letters / word in the sentence
        Dim iLength As Integer          'length of string
        Dim iWords As Integer           'number of words in sentence
        Dim iLetterCount As Integer     'count of letters
        Dim sWord As String             'current word
        Dim sPrevLetter As String       'previous letter
        Dim x As Integer                'loop counter
        Dim sLetter As String           'current letter from sentence
        Dim sLine As String = txtInput.Text.Trim.ToLower    'entered text  - no leading or trailing spaces - make all lowercase
        sLine = sLine.ToCharArray()
        Try
            iLength = sLine.Length + 1
            If iLength = 0 Then
                'nothing entered
                MessageBox.Show("Please enter a sentence.")
            Else

            End If
            sWord = ""
            sPrevLetter = ""
            For x = 0 To iLength - 1
                sLetter = sLine.Chars(x)  'get Next Letter
                Select Case sLetter
                    Case "a" To "z"
                        'we are in a word - increment letter counter
                        sWord &= sLetter
                    Case Else
                        'we are in a break between words
                        If sPrevLetter <> " " Then  'we don't want to count succesive spaces as a word
                            'add word to list
                            lstLetters.Items.Add(sWord)
                            iLength = iLength + 1
                            sWord = "" ' reset for next Word
                        End If
                End Select
                sPrevLetter = sLetter
                If sLetter = "" Then
                    iLength += 1
                End If
            Next
            If sWord <> "" Then
                lstLetters.Items.Add(sWord)
                iLength = iLength + 1
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        lblWordCount.Text = "Words Counted: " & lstLetters.Items.Count
        lblAverage.Text = "Average letters per Word: " & iLetterCount / iWords

    End Sub
Posted
Updated 18-Dec-10 5:30am
v2
Comments
Abhinav S 18-Dec-10 11:26am    
Where do you get the 'Index is out of bounds of the array' error?

You get NAN because you are dividing by 0 - iWords is never set anywhere in the code you've posted above.
 
Share this answer
 
Comments
Henry Minute 18-Dec-10 11:58am    
Wouldn't that give a DivideByZeroException rather than just giving NAN?
Sandeep Mewara 18-Dec-10 13:50pm    
Comment from OP:
I have corrected the "Index is out of bounds of the array" error. But I still cannot get an average to display. I realize that iWords needs to have a way to collect/contain all the words in the sentence. But how do I do that, so that I can divide the iLetterCount by iWords
Abhinav S 19-Dec-10 3:02am    
Henry - Good point. One would actually have to run this code to figure what could be really happening.
Use the split function to split the input.
result = sLine.split(" ")



Check this out:
VB
Public Function CalculateAverage(ByVal str As String) As Integer
       Dim TotalLetterCount As Integer = str.Replace(" ", "").Length

       While str <> str.Replace("  ", " ")
           str = str.Replace("  ", " ")
       End While
       Dim words() As String = str.Split(" ")
       Dim average As Integer = TotalLetterCount / words.Length
       Return average
   End Function


This function calculates the average.
it also contains the total nr of words (words.length)
and the words variable is a list of the words.

In your example you are not utilizing the built in functions of the language.
 
Share this answer
 
v3
Comments
Mark Berg 19-Dec-10 2:15am    
I tried using the split function you've written, but it didn't work. I'm still not getting an average to show up.
Magnus Gudmundsson 19-Dec-10 17:26pm    
I promise you Mark,provided that you are programming in vb.net (I think the code should work in vb.6, but im not sure :) ) if you put my function in your program (the function called CalculateAverage.)
and make a call to it att line 11 (and provided that you pass a string that is not empty),
it will return the average, I have tried it myself :)

Just paste the function into your code and att line 11
make a call to it in this way
lblAverage.Text = "Average letters per Word: " & cstr(CalculateAverage(sLine)
you will have to erase the second last line of your code, so that it does not override the value you get from my function.
Mark Berg 20-Dec-10 2:40am    
I did some reworking/coding to make the function call work--but it does work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900