Todd432
Todd432

Reputation: 79

Validating a string

I am trying to write a program where the user inputs a telephone number and a message is displayed if it contains a letter.

Module Module1

Sub Main()
    Dim TelNumber As String
    Dim Character As String
    TelNumber = Console.ReadLine()

    For Number = 0 To TelNumber.Length - 1
        Character = TelNumber.Substring(Number)
        If Integer.TryParse(Character, 0) <> 0 Then
            Console.WriteLine("It cannot contain a letter")
            Exit For
        End If
    Next

    Console.ReadLine()
End Sub

End Module

However with this code even it only works properly if the string contains less than 11 charcaters, after that even if it does not contain any letters it still displays the message. How do I fix this? Also I don't understand what the second parameter of the Integer.TryParse function represents?

Upvotes: 0

Views: 83

Answers (2)

Alex Shesterov
Alex Shesterov

Reputation: 27525

TelNumber.Substring(Number) does not just the Numberth character of the string. It returns the string with Number characters stripped off its beginning.

Thus, in the first step of the loop, TelNumber.Substring(0) returns the whole string. Then, Integer.TryParse() fails with integer overflow with long string.


Hint: simple string validation is a task for regular expressions. With regular expressions it will also be very easy to extend tel.number format to something like +4915771828000 or 12-34-56.

Imports System.Text.RegularExpressions

Module Module1

  Sub Main()
    Dim telNumber As String = Console.ReadLine()
    Dim telValidationRegex As Regex = New Regex("^\d+$")

    If NOT telValidationRegex.Match(telNumber).Success Then
      Console.WriteLine("Wrong telephone number format")
    End If

    Console.ReadLine()
  End Sub

End Module

Upvotes: 1

dsolimano
dsolimano

Reputation: 8986

I don't have a compiler handy, but I can tell you what I think this should look like. First let's look at Int32.TryParse's documentation

result Type: System.Int32

When this method returns, contains the 32-bit signed integer value equivalent of the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

So, the second parameter is supposed to be an integer variable that contains the result of trying to parse the string.

Then, let's look at substring

startIndex Type: System.Int32 The zero-based starting character position of a substring in this instance.

So what you're doing isn't looking at each character and trying to convert it to an integer. It's looking at the whole number, and trying to convert that to an integer. Then the whole number, except for the first character, and trying to convert that to a number. And so on.

I suspect you'll find that Int32 can only store numbers about 10 digits long (2^32, or 4294967296)

What you want to do is look at each character, something like this.

Module Module1
//Warning, did not try to compile
Sub Main()
    Dim TelNumber As String
    Dim Character As String
    TelNumber = Console.ReadLine()

    For Number = 0 To TelNumber.Length - 1
        Character = TelNumbert(Number)
        Dim OutputNumber as Integer
        If Not Integer.TryParse(Character, OutputNumber) Then
            Console.WriteLine("It cannot contain a letter")
            Exit For
        End If
    Next

    Console.ReadLine()
End Sub

End Module

Upvotes: 0

Related Questions