i want to make an app that count words from a text file and count words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nightknight
    New Member
    • Nov 2013
    • 1

    #1

    i want to make an app that count words from a text file and count words

    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 :



    Collapse | Copy Code


    Code:
    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
    
    'to count the recurrency of each word 
     Private Sub NumberOfEachWordToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumberOfEachWordToolStripMenuItem.Click
    
            txtnmb.Text = ""
    
            If Txtcount.Text = "" Then
                MsgBox("Please open a text file first !!!")
                Exit Sub
            End If
    
           
            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)
                String.ReferenceEquals(numbers, number(i))
                For j = 0 To (number.Length - 2)
                    If String.Compare(numbers(0), number(i)) = 0 Then
                        ac = ac + 1
    
                    End If
                Next
                Txtcounter2.Text = Txtcounter2.Text & numbers(i) & " : " & Val(ac) & vbNewLine
                ac = 0
            Next
    
    
        End Sub
        
    End Class


    anyway i m having a problem cause every word is appearing several time
    for example :
    hello world hello
    hello 2 times
    world 1 time
    hello 2 times
    and btw can anyone help me how to put them in the alphabetical order ?
    Last edited by Rabbit; Nov 3 '13, 10:54 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
Working...