By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,064 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,064 IT Pros & Developers. It's quick & easy.

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

P: 1
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


Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2.  
  3.  
  4. Public Class mainform
  5.     Public ac As Double
  6.     Public ch As Double
  7.  
  8.  
  9.     Private Sub mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.  
  11.     End Sub
  12.  
  13.  
  14. 'open file 
  15.  
  16.     Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
  17.         Dim filereader As StreamReader
  18.         Dim result As DialogResult
  19.         result = OpenFileDialog1.ShowDialog
  20.         If result = DialogResult.OK Then
  21.             filereader = New StreamReader(OpenFileDialog1.FileName)
  22.             Txtcount.Text = filereader.ReadToEnd
  23.             filereader.Close()
  24.         End If
  25.  
  26.     End Sub
  27.  
  28.     'word counter by counting the spaces 
  29.  
  30.     Private Sub TheNumberOfWordsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TheNumberOfWordsToolStripMenuItem.Click
  31.         txtnmb.Text = ""
  32.  
  33.         If Txtcount.Text = "" Then
  34.             MsgBox("Please open a text file first !!!")
  35.             Exit Sub
  36.         End If
  37.  
  38.         Dim number() As String
  39.  
  40.         ac = 0
  41.         ch = 0
  42.  
  43.         number = Txtcount.Text.Split(" ")
  44.  
  45.         'txtnmb.Text = ""
  46.         For i = 0 To (number.Length - 2)
  47.             ac = ac+ 1
  48.  
  49.         Next
  50.  
  51.         txtnmb.Text = txtnmb.Text & Val(ac) & vbTab
  52.  
  53.     End Sub
  54.  
  55. 'to count the recurrency of each word 
  56.  Private Sub NumberOfEachWordToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumberOfEachWordToolStripMenuItem.Click
  57.  
  58.         txtnmb.Text = ""
  59.  
  60.         If Txtcount.Text = "" Then
  61.             MsgBox("Please open a text file first !!!")
  62.             Exit Sub
  63.         End If
  64.  
  65.  
  66.         Dim ac As Integer
  67.         Dim ch As Integer
  68.         Dim number() As String
  69.         Dim numbers() As String
  70.  
  71.  
  72.  
  73.         ac = 0
  74.         ch = 0
  75.  
  76.         number = Txtcount.Text.Split(" ")
  77.         numbers = Txtcount.Text.Split(" ")
  78.         For i = 0 To (number.Length - 2)
  79.             String.ReferenceEquals(numbers, number(i))
  80.             For j = 0 To (number.Length - 2)
  81.                 If String.Compare(numbers(0), number(i)) = 0 Then
  82.                     ac = ac + 1
  83.  
  84.                 End If
  85.             Next
  86.             Txtcounter2.Text = Txtcounter2.Text & numbers(i) & " : " & Val(ac) & vbNewLine
  87.             ac = 0
  88.         Next
  89.  
  90.  
  91.     End Sub
  92.  
  93. 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 ?
Nov 3 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.