Results 1 to 13 of 13

Thread: [RESOLVED] Need help finishing an assignment

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Resolved [RESOLVED] Need help finishing an assignment

    I'm trying to finish an assignment for my Introduction to Visual Basic class.

    I have to program an application that will determine if a word entered has consecutive letters in the alphabet as a substring of itself. Three examples are T"HI""RST"Y, AF"GH"ANI"ST"AN, and "STU""DE"NT.

    This is what I have so far:

    Code:
    Public Class ConsecutiveLettersot
    
        'declare text input as class level variable
        Dim textInput As String
    
        'delcare substring locations as class level variables
        Dim locationOne As Integer
        Dim locationTwo As Integer
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    
    
            'clear read-only boxes
            lstConsecutiveLetters.Items.Clear()
            txtConcecutiveAmount.Text = ""
    
            'if the entry was numbers, display error
            If IsNumeric(txtEntry.Text) = False Then
                'entered text is not a number, delcare variable
                textInput = txtEntry.Text.ToUpper
            Else
                'entry was not letters
                MessageBox.Show("Please do not enter numbers.", "Error!")
                'return early
                Return
            End If
    
            'if the entry is less than 2 letters long
            If txtEntry.Text.Length < 2 Then
                MessageBox.Show("Entered text must be at least 2 letters long!", "Error!")
                'return early
                Return
            End If
    
            For i = 0 To ((textInput.Length) - 1)
    
                locationOne = Asc(textInput.Substring(i, 1))
                locationTwo = Asc(textInput.Substring(i + 1, 1))
    
                If locationTwo - locationOne = 1 Then
                    lstConsecutiveLetters.Items.Add(textInput.Substring(i, 1))
                End If
    
                i += 1
            Next
    
        End Sub
    
    End Class
    However, it doesn't run properly. It says I'm trying to refer to a location that is out of bounds, although I don't see why.

    Any help would be greatly appreciated.

    Thank you.

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Need help finishing an assignment

    When you do this:

    Code:
    locationTwo = Asc(textInput.Substring(i + 1, 1))
    On the last character, you are going to be out of bounds, as you are saying take the last character and look at the next one.

    For what you are doing, you can probably change your loop to:

    Code:
    For i = 0 To ((textInput.Length) - 2)
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Need help finishing an assignment

    Code:
    'if the entry was numbers, display error
            Else
                'entry was not letters
               MessageBox.Show("Please do not enter numbers.", "Error!")
            End If
    Before we go further you display value is a number each time?

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Need help finishing an assignment

    Quote Originally Posted by ident View Post
    Before we go further you display value is a number each time?
    No, the instructor wanted a prompt to appear if anything other than letters was entered into the input box.

  5. #5
    Addicted Member
    Join Date
    Nov 2011
    Posts
    129

    Re: Need help finishing an assignment

    I noticed you have an i += 1 in your loop. You need to remove that. The for...next already iterates i for you.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Need help finishing an assignment

    Quote Originally Posted by Ashaelon View Post
    I noticed you have an i += 1 in your loop. You need to remove that. The for...next already iterates i for you.
    Good catch, that would explain why it was only printing every other letter.

    Focusing just on the For-Next loop, I'm thinking I would have to use another nested loop, to cycle through the rest of the word after consecutive order is found.

    But how would I make the loop begin from that particular position? I'm not sure how to declare the start of the loop if "i" can't be used again.

  7. #7
    Addicted Member
    Join Date
    Nov 2011
    Posts
    129

    Re: Need help finishing an assignment

    An example of that would be
    Code:
    For j as integer = i to textInput.length - 2
        'code here
    Next

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Need help finishing an assignment

    Quote Originally Posted by Ashaelon View Post
    An example of that would be
    Code:
    For j as integer = i to textInput.length - 2
        'code here
    Next
    That's easier than I expected, I wasn't thinking I could simply refer back to "i."

    How could I make this program record the last letter in the consecutive order, without referencing a position that's out of bounds?
    Last edited by Barqs; Nov 17th, 2011 at 05:26 PM. Reason: Grammar

  9. #9
    Addicted Member
    Join Date
    Nov 2011
    Posts
    129

    Re: Need help finishing an assignment

    Here is what I came up with. There may be a better way, but it's all I've got right now.

    - Edit -

    Code removed because it failed the simplest test: abcdef. Sorry for jumping the gun on that code.
    Last edited by Ashaelon; Nov 17th, 2011 at 05:39 PM. Reason: code fails

  10. #10
    Addicted Member
    Join Date
    Nov 2011
    Posts
    129

    Re: Need help finishing an assignment

    Try this

    Code:
        'declare text input as class level variable
        Dim textInput As String
    
        'delcare substring locations as class level variables
        Dim locationOne As Integer
        Dim locationTwo As Integer
        Dim sConsecutiveLetters As String
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    
    
            'clear read-only boxes
            lstConsecutiveLetters.Items.Clear()
            txtConcecutiveAmount.Text = ""
            sConsecutiveLetters = ""
    
            'if the entry was numbers, display error
            If IsNumeric(txtEntry.Text) = False Then
                'entered text is not a number, delcare variable
                textInput = txtEntry.Text.ToUpper
            Else
                'entry was not letters
                MessageBox.Show("Please do not enter numbers.", "Error!")
                'return early
                Return
            End If
    
            'if the entry is less than 2 letters long
            If txtEntry.Text.Length < 2 Then
                MessageBox.Show("Entered text must be at least 2 letters long!", "Error!")
                'return early
                Return
            End If
    
            For i = 0 To ((textInput.Length) - 2)
                Debug.WriteLine(textInput.Substring(i, 1) & vbCrLf & textInput.Substring(i + 1, 1) & vbCrLf)
                locationOne = Asc(textInput.Substring(i, 1))
                locationTwo = Asc(textInput.Substring(i + 1, 1))
    
                If locationTwo - locationOne = 1 Then
                    If Not sConsecutiveLetters.Length > 0 Then sConsecutiveLetters = textInput.Substring(i, 1)
                    sConsecutiveLetters += textInput.Substring(i + 1, 1)
                Else
                    If sConsecutiveLetters.Length > 0 Then
                        For j As Integer = 0 To sConsecutiveLetters.Length - 1
                            lstConsecutiveLetters.Items.Add(sConsecutiveLetters.Substring(j, 1))
                        Next
    
                        sConsecutiveLetters = ""
                    End If
                End If
            Next
    
            If sConsecutiveLetters.Length > 0 Then
                For j As Integer = 0 To sConsecutiveLetters.Length - 1
                    lstConsecutiveLetters.Items.Add(sConsecutiveLetters.Substring(j, 1))
                Next
            End If
        End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Need help finishing an assignment

    Quote Originally Posted by Ashaelon View Post
    Try this
    That helps tremendously, thank you.

    Also, while I have your attention..

    what does this line do?
    Code:
    Debug.WriteLine(textInput.Substring(i, 1) & vbCrLf & textInput.Substring(i + 1, 1) & vbCrLf)

  12. #12
    Addicted Member
    Join Date
    Nov 2011
    Posts
    129

    Re: Need help finishing an assignment

    It's used for writing debug information to the Immediate Window so that you can see what is taking place through each step (it has other purposes too, but that the simplest to explain). I put it in to see what 2 values it was comparing (that's how i realized you had the i += 1 in the loop). If you haven't learned it yet, you can remove it (your professor might not be happy seeing it there).

    Here is a link to explain more.
    http://msdn.microsoft.com/en-us/libr...writeline.aspx

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Need help finishing an assignment

    Will do.

    Thanks again!

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