Reputation: 3
I need help in Vb.net, how can I traverse through an 8 digit integer and check whether
I input the 8 digit as string as its a requirement then convert into a string.
Imports System.console
Public Class residentNumberCheck
Dim cardNumber As String
Public Sub inputNumberAndCheck()
writeline("Enter the 8 Digit Resident Card Number")
cardNumber = readline()
While (cardNumber.length() <> 8)
writeline("The Resident Card Number is Invalid. It does not contain 8 digits... Please enter again")
cardNumber = readline()
End While
End Sub
Public Sub sameThreeNumber()
writeline("")
writeline("...Checking for 3 consecutive numbers...")
writeline("")
Dim Num As Integer = Integer.parse(cardNumber)
Dim temp As Integer = Num
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim checker As Boolean = False
While (Num > 0)
i = Num Mod 10
Num = Num \ 10
j = Num Mod 10
temp = temp \ 10
k = temp Mod 10
Num = temp \ 10
temp = Num \ 10
If (i = j And j = k) Then
writeline(" ****The Number is a Special Number as the Number {0} occurs three consecutive times****", i)
checker = True
Return
End If
End While
If (checker = False) Then
writeline("The Number does not have 3 consecutive numbers")
End If
End Sub
Public Sub ConsecutiveNumber()
writeline("")
writeline("...Checking for consecutive digits in increasing or decreasing...")
writeline("")
Dim Num As Integer = Integer.parse(cardNumber)
Dim temp As Integer = Num
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim checker As Boolean = False
While (Num > 0)
i = Num Mod 10
Num = Num \ 10
j = Num Mod 10
temp = temp \ 10
k = temp Mod 10
Num = temp \ 10
temp = Num \ 10
check1:
If (i = j + 1) Then
If (j = k + 1) Then
checker = True
writeline(" ****The Number is a Special Number as it has {0}, {1}, {2} consecutive numbers****", k, j, i)
Return
End If
End If
check2:
If (i = j - 1) Then
If (j = k - 1) Then
checker = True
writeline(" ****The Number is a Special Number as it has {0}, {1}, {2} consecutive numbers****", k, j, i)
Return
End If
End If
End While
If (checker = False) Then
writeline("The Number does not contain consecutive digits in increasing or decreasing")
End If
End Sub
Public Sub checksimilarfourdigits()
writeline("")
writeline("...Checking if a number is repeated four times...")
writeline("")
Dim Num As Integer = Integer.parse(cardNumber)
Dim temp As Integer
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim j As Integer
Dim counter As Integer = 0
Dim checker As Boolean = False
While (Num > 0)
logic:
i = Num Mod 10
Num = Num \ 10
j = Num Mod 10
Num = Num \ 10
check:
If (i = j) Then
counter = counter + 1
temp = i
Else
temp = j
GoTo logic
End If
If (temp = i) Then
counter = counter + 1
x = temp
y = 0
End If
If (temp = j) Then
counter = counter + 1
y = temp
x = 0
End If
If (counter >= 4) Then
If (x = 0) Then
writeline(" ****The Number is a Special Number as the number {0} repeats four times or more****", y)
checker = True
Return
End If
If (y = 0) Then
writeline(" ****The Number is a Special Number as the number {0} repeats four times or more****", x)
checker = True
Return
End If
End If
End While
If (checker = False) Then
writeline("The Number does not contain 4 equal digits")
End If
End Sub
Public Shared Sub main()
Dim user As residentNumberCheck = New residentNumberCheck()
user.inputNumberAndCheck()
user.sameThreeNumber()
user.ConsecutiveNumber()
user.checksimilarfourdigits()
End Sub
End Class
Upvotes: 0
Views: 130
Reputation: 452
If you want to compact your code you may use Regex class:
Dim cardNumber As String = TextBox1.Text
' (\d) finds digit (\d)\1 finds digit repeated once, here \1 makes
' reference to first group (), this is, (\d)
' (\d)\1{2,} finds digit repeated at least 2 times
Dim mc As MatchCollection = New Regex("(\d)\1{2,}").Matches(cardNumber)
If mc.Count Then ' there are at least 3 consecutive equal #s
Trace.WriteLine(String.Format("Number {0} occurs {1} times", mc(0).Value, mc(0).Value.Length))
End If
mc = New Regex("\d").Matches(cardNumber)
' each mc() contains one digit
Dim incr As Int32=0, indI As Int32 = -1
Dim decr As Int32=0, indD As Int32 = -1
For i As Int32 = 0 To mc.Count - 1
Dim curr As Int32 = Int32.Parse(mc(i).Value)
If i < mc.Count - 1 Then ' there is a next number
' if current+1=next# =>increment count & save index
If curr + 1 = Int32.Parse(mc(i + 1).Value) Then
If indI = -1 Then indI = i
incr += 1 : If incr = 2 Then Exit For
Else
incr = 0 : indI = -1
End If
End If
If i Then ' there is a previous #
' if current+1=previous# =>previous incr. count & save index
If curr + 1 = Int32.Parse(mc(i - 1).Value) Then
If indD = -1 Then indD = i
decr += 1 : If decr = 2 Then Exit For
Else
decr = 0 : indD = -1
End If
End If
Next
If incr = 2 Then
Trace.WriteLine(String.Format("{0}{1}{2} Consecutive numbers", mc(indI), mc(indI + 1), mc(indI + 2)))
ElseIf decr = 2 Then
Trace.WriteLine(String.Format("{0}{1}{2} Consecutive numbers", mc(indD - 1), mc(indD), mc(indD + 1)))
End If
Upvotes: 1