I'm trying to find the count of the digits, the lowest digit, the highest digit, and the average of all the digits in a string.
This is what I was able to come up with so far
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 0
Dim minimumInt As Integer
Dim maximumInt As Integer
Dim averageInt As Single
For Each c As Char In TextBox1.Text
If Char.IsDigit(c) Then
i = i + 1
If (Asc(c) < minimumInt) Then
minimumInt = Asc(c)
ElseIf (Asc(c) > maximumInt) Then
maximumInt = Asc(c)
End If
averageInt = averageInt + Asc(c)
averageInt = averageInt / i
End If
Next
MessageBox.Show("Total Digits: " & i & ControlChars.CrLf & "Average Number: " & averageInt & ControlChars.CrLf & "Minimum Number: " & minimumInt & ControlChars.CrLf & "Maximum Number: " & maximumInt)
End Sub
End Class
I believe the issue is involving converting Chars to Integers.
For example, when TextBox1 contains "X13210AS", I get this http://i.imgur.com/5aaOWC0.png
When I should be getting an average of 1.4 and highest digit 3.
Any ideas on how to fix this while still using the majority of my code?