Results 1 to 6 of 6

Thread: [RESOLVED] Array out of range.

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Resolved [RESOLVED] Array out of range.

    Hi I am trying to make some code to collect tokens of a string. but I keep getting an out of range error, tho it works with some expressions
    here my code.

    Code:
    Public Class Form1
    
        Private Enum Tokypes
            None = 0
            Digit = 1
            Sysmbol = 2
        End Enum
    
        'Private Expression As String = "8 * 2 + (10+5)" 'Works fine on this expression.
        Private Expression As String = "8 * 2 + 1" ' VB Crys on this one why
    
        Private Pos As Integer = 0
        Dim sToken As String = vbNullString
        Dim sTokType As Tokypes
    
        Private Function isDelim(ByVal c As Char) As Boolean
            If (("+-/*%^=()".IndexOf(c) <> -1)) Then
                Return True
            End If
            Return False
        End Function
    
        Private Sub GetTokens()
            sToken = vbNullString
            sTokType = Tokypes.None
    
            If (Pos = Expression.Length) Then
                sToken = vbNullString
                sTokType = Tokypes.None
                Return
            End If
    
            'Skip white
            While (Pos < Expression.Length) And Char.IsWhiteSpace(Expression(Pos))
                Pos += 1
            End While
            '
            If isDelim(Expression(Pos)) Then
                sToken = sToken & Expression(Pos)
                Pos += 1
                'Token Type.
                sTokType = Tokypes.Sysmbol
            ElseIf Char.IsDigit(Expression(Pos)) Then
                'Thowing error here - Index was outside the bounds of the array.
                While Char.IsDigit((Expression(Pos)))
                    sToken = sToken & Expression(Pos)
                    Pos += 1
                End While
                If Pos = Expression.Length Then Return
                'Token Type.
                sTokType = Tokypes.Digit
            End If
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            GetTokens()
    
            While sTokType <> Tokypes.None
                MsgBox(sToken)
                GetTokens()
            End While
    
        End Sub
    End Class
    If you run the code with this expression eveything is fine "8 * 2 + (10+5)"
    But if you run it with this you get an out of range error. 8 * 2 + 1
    I don't see why cos the first expression has more length than the second.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Array out of range.

    your problem occurs in the code below when you enter the loop and Pos = 8. When Pos is 8,Char.IsDigit is true because Expression(8)="1". In the loop you then increment Pos, loop and look at Expression(9) which doesn't exist because the upper index of the string array is 8.

    You need to change your code to test Pos and jump out of the While or rearrange your logic.

    Code:
     While Char.IsDigit((Expression(Pos)))
                    sToken = sToken & Expression(Pos)
                    Pos += 1
                End While
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: Array out of range.

    Thanks

    Think I fixed it I appened an extra white space to the end of the expression and it seems to work. I left the code here in case someone else may need it, updated for Alpha.

    Code:
    Public Class Form1
    
        Private Enum Tokypes
            None = 0
            Digit = 1
            Alpha = 2
            Sysmbol = 3
        End Enum
    
        Private Expression As String = "PI+8 * 2 + 500 *9 * SIN(3)"
    
        Private Pos As Integer = 0
        Dim sToken As String = vbNullString
        Dim sTokType As Tokypes
    
        Private Function isDelim(ByVal c As Char) As Boolean
            If ((" +-/*%^=()".IndexOf(c) <> -1)) Then
                Return True
            End If
            Return False
        End Function
    
        Private Sub GetTokens()
            sToken = vbNullString
            sTokType = Tokypes.None
    
            While Pos < Expression.Length AndAlso Char.IsWhiteSpace(Expression(Pos))
                Pos += 1
            End While
    
            If (Pos = Expression.Length) Then
                sToken = vbNullString
                sTokType = Tokypes.None
                Return
            End If
    
            If isDelim(Expression(Pos)) Then
                sToken = Expression(Pos)
                Pos += 1
                'Token Type.
                sTokType = Tokypes.Sysmbol
            ElseIf Char.IsLetter(Expression(Pos)) Then
                While Not isDelim(Expression(Pos))
                    sToken = sToken & Expression(Pos)
                    Pos += 1
                    If (Pos >= Expression.Length) Then
                        Return
                    End If
                End While
                'Token Type.
                sTokType = Tokypes.Alpha
            ElseIf Char.IsDigit(Expression(Pos)) Then
                While Not isDelim(Expression(Pos))
                    sToken = sToken & Expression(Pos)
                    Pos += 1
                    If (Pos >= Expression.Length) Then
                        Return
                    End If
                End While
                'Token Type.
                sTokType = Tokypes.Digit
            Else
    
            End If
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Pos = 0
            GetTokens()
    
            While sTokType <> Tokypes.None
                MsgBox(sToken)
                GetTokens()
            End While
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Expression = Expression & " "
        End Sub
    End Class

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Array out of range.

    What is this code supposed to accomplish?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: [RESOLVED] Array out of range.

    Quote Originally Posted by dbasnett View Post
    What is this code supposed to accomplish?
    tokenizer for a math evaluator I am going to make.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Array out of range.

    I thought so. This may be useful Parsing Infix
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width