[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.
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)
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?
Re: Need help finishing an assignment
Quote:
Originally Posted by
ident
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.
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.
Re: Need help finishing an assignment
Quote:
Originally Posted by
Ashaelon
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.
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
Re: Need help finishing an assignment
Quote:
Originally Posted by
Ashaelon
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?
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.
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
Re: Need help finishing an assignment
Quote:
Originally Posted by
Ashaelon
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)
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
Re: Need help finishing an assignment