Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to make an app that count words from a text file and count words and the number of each word then to list it in alphabetical order for example:
text file : hello world hi world
number of words : 4
number of each word :
hello 1 time
hi 1 time
world 2 time

i manage to write a code that manage to open a text file and to count the number of words but i m not managing to how to count each word and then display them in alphabetical order ,the code till now :



Imports System.IO


Public Class mainform
    Public ac As Double
    Public ch As Double
    

    Private Sub mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


'open file 

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim filereader As StreamReader
        Dim result As DialogResult
        result = OpenFileDialog1.ShowDialog
        If result = DialogResult.OK Then
            filereader = New StreamReader(OpenFileDialog1.FileName)
            Txtcount.Text = filereader.ReadToEnd
            filereader.Close()
        End If

    End Sub

    'word counter by counting the spaces 

    Private Sub TheNumberOfWordsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TheNumberOfWordsToolStripMenuItem.Click
        txtnmb.Text = ""

        If Txtcount.Text = "" Then
            MsgBox("Please open a text file first !!!")
            Exit Sub
        End If

        Dim number() As String

        ac = 0
        ch = 0

        number = Txtcount.Text.Split(" ")

        'txtnmb.Text = ""
        For i = 0 To (number.Length - 2)
            ac = ac+ 1
            
        Next
        
        txtnmb.Text = txtnmb.Text & Val(ac) & vbTab

    End Sub

    
End Class
Posted

1 solution

It's not too difficult, but as this is homework, I'll not give you any code.

Create a Dictionary, of a string key, and integer value.
Loop through each word in your text, and use the Dictionary.ContainsKey method to check if the word is already present. If it isn't, add it with a value of one. If it is, just increment the current value by one. At the end of the loop, you have a dictionary which contains each word, with the count of how many times it appears.

Edit: corrected weird as word...
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 2-Nov-13 17:52pm    
"check if the weird is already present."
Since this is the Q&A the weird is always present.
Interesting side note: I think this misspelling does give one a sense of how the word "word" might be pronounced in your local dialect.
I might be wrong, but I've experienced similar glitches in my spelling where the pronounciation takes impact on the spelling of a homophone.

Cheers!
OriginalGriff 2-Nov-13 18:16pm    
Tablet *£#&&£ng keyboards! :laugh:
The worst thing about the android one on my Nexus 7 is it keeps spelling code as coffee...
Member 10250266 2-Nov-13 19:41pm    
well i know the principles how to solve it but i don't know how to write syntax i m having a lot of errors i can do it in c++ but i have to do this assignment in vb this is why i m having trouble doing it
Member 10250266 2-Nov-13 20:15pm    
plz i did make this code to count the re-currency of a word in a text file but it giving me an error on :
numbers = Val(number(i)) 'the error is value of type string cannot be converted to 1-dimensional array of string

the same error is in :
numbers = number(i) 'overload resolution failed because no accessible '=' can be called with these arguments ....

this is the entire code of the sub :

Dim ac As Integer
Dim ch As Integer
Dim number() As String
Dim numbers() As String



ac = 0
ch = 0

number = Txtcount.Text.Split(" ")
'numbers = Txtcount.Text.Split(" ")

For i = 0 To (number.Length - 2)
numbers = Val(number(i)) 'it is not taking it value
For j = 0 To (number.Length - 2)
If numbers = number(i) Then 'neither here it giving me an error
ac = ac + 1
End If
Next
Txtcounter2.Text = Txtcounter2.Text & numbers(i) & " : " & Val(ac) & vbNewLine
ac = 0
Next
OriginalGriff 3-Nov-13 3:55am    
Well, do - the compiler is exactly right.
You declared "numbers" as an array:
Dim numbers() As String
Then in your loop you try to set it to a single integer:
numbers = Val(number(i)) 'it is not taking it value
Probably, what you need to do is set an element of "numbers" to the value:
numbers(i) = Val(number(i))
But since "numbers" is an array of strings, that isn't going to be helpful either.

Try an experiment: grab a sheet of paper and a pencil, and do it manually on that. That should give you a better idea of what processes and data you need to store - because that code just isn't going to work! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900