Question:
In Visual basic, I am reading unique words from a text file. These textfiles may also have numbers in them.?
thegreatone36703
2011-05-02 10:51:10 UTC
My question is how do I read through the text file and every time I come across a number, it increments that number. For example, if I have three 5s and six 3s, the count should be Five(3) and Three(6)
Three answers:
texasmaverick
2011-05-02 12:29:54 UTC
Place the following code in your existing code (after you read a word)





Dim String1, String2 As String

Dim Num As Integer

Dim num0 as Integer=0

Dim num1 As Integer=0

Dim num2 As Integer=0

Dim num3 as Integer=0

Dim num4 As Integer=0

Dim num5 As Integer=0

Dim num6 As Integer=0

Dim num7 as Integer=0

Dim num8 As Integer=0

Dim num9 As Integer=0



String1 = (word read from file which must be a string)

For Z = 1 To Len(String1)

String2 = Mid(String1, Z, 1)

Num = Asc(String2)

If Num > 47 And Num < 58 Then

If Num=48 then

num0=num0+1

If Num=49 then

num1=num1+1

If Num=50 then

num2=num2+1

If Num=51 then

num3=num3+1

If Num=52 then

num4=num4+1

If Num=53 then

num5=num5+1

If Num=54 then

num6=num6+1

If Num=55 then

num7=num7+1

If Num=56 then

num8=num8+1

If Num=57 then

num9=num9+1

End If

Next Z



You can now use num0 through num9 as the number of 0s through 9s.



TexMav
MarkG
2011-05-05 17:08:20 UTC
There are a 1000 ways to skin a cat. So here is yet another way to do what you ask using a collection and its ability to use a string as a key to identify an item within the collection. Using a key assigned to an individual numeric character will let the collection go directly to a particular item rather than you having to write code to test each number to see where it should go.



Place a button on a form1 and copy the following code into the code window



Public Class Form1



Private numCollection As New Collection





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



CountNumbers(" this is a 333333 test of 555 counting 77 just 2222 then 000 numbers ")

DisplayNumberCount()



End Sub

Private Sub CountNumbers(ByVal str As String)



Dim idx, temp As Integer

Dim myKey As String







numCollection.Clear()



'Create 10 items in the collection that will track

'the numbers 0 to 9 and define their key values

'and initalize the stored value to 0.

'

'Keys provide an alternative to a numeric index

'Keys are strings and can be 1 of more characters

'

'A collection is like an array but adds some additional

'features (like keys). However collections are read only

'so to change a value you delete the old collection item

'and add a new item in its place.



For idx = 0 To 9

'create key'ed collection items

numCollection.Add(0, idx.ToString)



Next



'

'using the MID function get one character at a time from the

'string and use it as a key for numCollection.



'a TRY / CATCH block is used to handle errors that

'will be generated when a key value is not found in

'the collection



For idx = 1 To str.Length 'loop through the entire string



myKey = Mid(str, idx, 1) 'get a single character from the string



Try 'TRY / Catch error handler

temp = numCollection(myKey) 'read the stored count



temp = temp + 1 ' increment the count



numCollection.Remove(myKey) 'delete the old count



numCollection.Add(temp, myKey) 'replace the updated count to the key location

Catch ex As Exception

'the character used as a key was not found in the collection

' do nothing. Continue looping through rest of string

End Try



Next



End Sub





Private Sub DisplayNumberCount()

Dim str, temp As String

Dim idx As Integer





'vbcrlf is a built in constant used to make text go to the next line

str = "Counts:" & vbCrLf



For idx = 0 To 9

temp = numCollection(idx.ToString)

str = str & " The number " & idx & " has " & temp & vbCrLf



Next



MsgBox(str)

End Sub

End Class
Gasman
2011-05-02 17:57:33 UTC
Use the number found as the index to an arrayof numbers so arrNum(5) holds the count of 5's and increment that element.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...