Jump to content

Testing string/integers for numbers then counting


Recommended Posts

At the minute I'm using a form with a textbox which asks a user to input a name, then what I have to do is test the input to see if any characters are numeric, this part I have done using a For loop and a MsgBox saying "A number was found" followed by Exit Sub to end the program, but the problem is I can't think of how I would test the input to see how many characters are numbers and then display this count in a MsgBox.

Anyway, here's the code I'm using if you'd need it.

Dim OneCharacter As Char
Dim FirstName As String
Dim i As Integer
Dim TextLength As Integer
Dim NumberCount As Integer


FirstName = Trim(txtChars.Text)

TextLength = FirstName.Length

For i = 0 To TextLength - 1

If IsNumeric(OneCharacter) Then

MsgBox("A number was found")
Exit Sub

End If

OneCharacter = FirstName.Chars(i)

Next i


End Sub
End Class

Would it be best to add another For loop inside the If statement?

Link to comment
Share on other sites

First, you need to think the thing through - what do you want to achieve?

You want to know

  1. if there are any numbers in the string?
  2. how many numbers there are in the string?

This is basically the same question; if the result from the 2nd question above is 0, then there are none, otherwise there are.

Meaning: you will have to complete the loop; not exit when you find a number, but count it instead.

P.S. Your Programming sub-forum is being discussed by forum staff; I think it will be coming soon B)

Link to comment
Share on other sites

P.S. Your Programming sub-forum is being discussed by forum staff; I think it will be coming soon B)

:) Ta for letting me know. :)

I need to use OneCharacter As Char because it won't let me display the message box if I use FirstName As String.

What I want to do is count how many numbers are in OneCharacter/FirstName and the display them in a message box. With the code I posted before, it finds numbers in OneCharacter and displays a message saying "A number was found", the only thing is I don't know how to count them.

Edit:- I don't know why I put OneCharacter at the bottom there though. :huh:

Link to comment
Share on other sites

... the only thing is I don't know how to count them.

How about something like this

		For i = 0 To TextLength - 1

OneCharacter = FirstName.Chars(i)

If IsNumeric(OneCharacter) Then

NumberCount = NumberCount + 1

End If

Next i

Then at the end something like

		If NumberCount > 0 Then

MsgBox(NumberCount, " number(s) found")

End If

I don't know if the format is 100% right, as I have never done any VB before.

P.S. I also don't know if NumberCount needs to be initialized to 0, or if VB does that automatically.

Link to comment
Share on other sites

I tried it but it didn't work how I needed it to.

I have now done something which brings up a message box saying "There are 0 numbers"

Dim OneCharacter As Char
Dim FirstName As String
Dim i As Integer
Dim TextLength As Integer
Dim NumberCount As Integer

FirstName = Trim(txtChars.Text)
TextLength = FirstName.Length

For i = 0 To NumberCount
OneCharacter = FirstName.Chars(i)
NumberCount = IsNumeric(OneCharacter)

MsgBox("There are " & NumberCount & " numbers")

Next i


End Sub

I don't know why I'm finding this so hard. :( I went through the tutorial really easy until now - Is it supposed to be this confusing? :D

Edit:-

Oh, and I have set the text property of the text box "txtChars" to "george123" so there are numbers in the FirstName

Link to comment
Share on other sites

Sorry for the multi-posting! :(

I've finally done it after drinking lots of tea, and smoking plenty cigarettes whilst on a "break" :)

I was putting things in the wrong places and got myself confused, based on your code you suggested, Pat, I've been able to do it. :)

	   Dim OneCharacter As Char
Dim FirstName As String
Dim i As Integer
Dim TextLength As Integer
Dim NumberCount As Integer

FirstName = Trim(txtChars.Text)
TextLength = FirstName.Length


For i = 0 To TextLength - 1

OneCharacter = FirstName.Chars(i)

If IsNumeric(OneCharacter) Then

NumberCount = NumberCount + 1

End If

Next i

If NumberCount > 0 Then
MsgBox(NumberCount & " numbers were found")

End If

Thanks :) I can move on now. :D

Link to comment
Share on other sites

Some comments on the two version of the code (For loop).

While my short version certainly looks elegant, it is not necessarily the best code.

The earlier code, with several lines, is indeed better, because it is easier to read & understand. It is one of the important things when writing productive code, that others can read and understand it.

Anyway, waiting for your next challenge... :flowers:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy