user2536008
user2536008

Reputation: 215

Conditionally exit or continue the loop in vb.net

I have the following sub below. What it does is that it takes the input from the textbox that the user enters, substitute each letter with a specific numeric value, display the total for each word and then the total for the entire input. For example, if the user types aa bbb e, the output in txtbox5 is:

aa = 40
bbb = 90
e = 40
Total 170

That works fine if the user inputs one long sentence. So I want to calculate each sentence separately in the same fashion and the delimiter for the sentence could be a period or comma. If the user types aa bbb. ff ee. The output should

aa = 40
ccc = 90
Total for the 1st sentence = 120
ff = 100
ee = 80
Total for the 2nd sentence = 180
and so forth

Private Sub Calculate(ByVal input As String)
    Dim total As Integer = 0
    Dim wordTotal As Integer
    Dim dicLetters As New Dictionary(Of Char, Integer)

    dicLetters.Add("A", 20)
    dicLetters.Add("B", 30)
    dicLetters.Add("E", 40)
    dicLetters.Add("F", 50)

    Dim charValue As Integer

    For Each word As String In input.Split(New Char() {" "})

        wordTotal = 0

        For Each character As Char In word

            wordTotal += If(dicLetters.TryGetValue(character, charValue) = _
            True, dicLetters(character), 0)
        Next

        total += wordTotal

        txtBox5.Text += word.PadRight(12) + " = " + _
        wordTotal.ToString().PadLeft(5) + vbNewLine
    Next

    txtBox5.Text += "Total:".PadRight(12) + " = " + _
    total.ToString().PadLeft(5)
End Sub

Upvotes: 1

Views: 244

Answers (1)

tinstaafl
tinstaafl

Reputation: 6948

Use the Split method to create an array of sentences. It will accept an array of delimiters. Iterate through the array and pass each sentence and the index of the sentence, to your sub routine. In your sub routine add an integer argument to use as the sentence number, and change the output string.

Something like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Sentences() As String = TextBox1.Text.Split({","c,"."c})
    For I = 0 to Sentences.Count-1
        Calculate(Sentences(I), I+1)
    Next
End Sub

Private Sub Calculate(ByVal input As String, ByVal index As Integer)
    Dim total As Integer = 0
    Dim wordTotal As Integer
    Dim dicLetters As New Dictionary(Of Char, Integer)

    dicLetters.Add("A", 20)
    dicLetters.Add("B", 30)
    dicLetters.Add("E", 40)
    dicLetters.Add("F", 50)

    Dim charValue As Integer

    For Each word As String In input.Split(New Char() {" "})

        wordTotal = 0

        For Each character As Char In word

            wordTotal += If(dicLetters.TryGetValue(character, charValue) = _
            True, dicLetters(character), 0)
        Next

        total += wordTotal

        txtBox5.Text += word.PadRight(12) + " = " + _
        wordTotal.ToString().PadLeft(5) + vbNewLine
    Next

    txtBox5.Text += "Total for sentence " + index.ToString +" :" + " = " + _
    total.ToString().PadLeft(5)
End Sub

Upvotes: 2

Related Questions