Connecting Tech Pros Worldwide Forums | Help | Site Map

I dont want objreader to read spaces or character

Newbie
 
Join Date: Feb 2008
Posts: 15
#1: Feb 16 '08
I am trying to write a program that reads in a text file and prints out how many # of each letter shows up for example if my text files is "Good food?"

there will be 2- d, 1- f, 1 -g, 4- o.

The problem that I am having when I run through the loops is that the spaces and characters are counting for A. so in the case of "Good Food?"
there will be 2- d, 1- f, 1 -g, 4- 0 and 2 - A.

Here is my sub with the loop.


Expand|Select|Wrap|Line Numbers
  1. Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
  2.  
  3.  Dim i, j, k, l, x As Integer
  4.     Dim lettercount(26) As Integer
  5.     Dim digramCount(26, 26) As Integer
  6.     Dim letterArray(26) As Label
  7.         Dim objReader As New StreamReader("test.txt")
  8.         Dim sLine As String = ""
  9.         Dim arrText As New ArrayList()
  10.  
  11.         Do
  12.             sLine = objReader.ReadLine()
  13.             If Not sLine Is Nothing Then
  14.                 arrText.Add(sLine)
  15.             End If
  16.         Loop Until sLine Is Nothing
  17.         objReader.Close()
  18.  
  19.         For Each sLine In arrText
  20.             Console.WriteLine(sLine.ToLower)
  21.         Next
  22.         Console.ReadLine()
  23.  
  24.             For j = 0 To lettercount.Length - 1
  25.                 lettercount(j) = 0
  26.             Next
  27.  
  28.             For k = 0 To 25
  29.                 For l = 0 To 25
  30.                     digramCount(k, l) = 0
  31.                 Next
  32.             Next
  33.  
  34.             For i = 1 To sLine.Length
  35.                 Dim int As Integer
  36.                 int = selectletterCase(sLine.Substring(i - 1, 1))
  37.                 lettercount(int) += 1
  38.             Next
  39.  
  40.             For x = 0 To 25
  41.                 letterArray(x).Text = lettercount(x).ToString
  42.                 MsgBox(letterArray(x).ToString)
  43.             Next
  44.         End If
  45.     End SubPrivate Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmit.Click

any help is appreciated!

mafaisal's Avatar
Familiar Sight
 
Join Date: Sep 2007
Location: Cochin,India
Posts: 138
#2: Feb 16 '08

re: I dont want objreader to read spaces or character


Hello

Replace All Spaces

eg
Expand|Select|Wrap|Line Numbers
  1. Replace(string, " ", "")
  2.  
Now All Spaces are remove then Try

Faisal

Quote:

Originally Posted by firebirds98

I am trying to write a program that reads in a text file and prints out how many # of each letter shows up for example if my text files is "Good food?"

there will be 2- d, 1- f, 1 -g, 4- o.

The problem that I am having when I run through the loops is that the spaces and characters are counting for A. so in the case of "Good Food?"
there will be 2- d, 1- f, 1 -g, 4- 0 and 2 - A.

Here is my sub with the loop.


Expand|Select|Wrap|Line Numbers
  1. Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
  2.  
  3.  Dim i, j, k, l, x As Integer
  4.     Dim lettercount(26) As Integer
  5.     Dim digramCount(26, 26) As Integer
  6.     Dim letterArray(26) As Label
  7.         Dim objReader As New StreamReader("test.txt")
  8.         Dim sLine As String = ""
  9.         Dim arrText As New ArrayList()
  10.  
  11.         Do
  12.             sLine = objReader.ReadLine()
  13.             If Not sLine Is Nothing Then
  14.                 arrText.Add(sLine)
  15.             End If
  16.         Loop Until sLine Is Nothing
  17.         objReader.Close()
  18.  
  19.         For Each sLine In arrText
  20.             Console.WriteLine(sLine.ToLower)
  21.         Next
  22.         Console.ReadLine()
  23.  
  24.             For j = 0 To lettercount.Length - 1
  25.                 lettercount(j) = 0
  26.             Next
  27.  
  28.             For k = 0 To 25
  29.                 For l = 0 To 25
  30.                     digramCount(k, l) = 0
  31.                 Next
  32.             Next
  33.  
  34.             For i = 1 To sLine.Length
  35.                 Dim int As Integer
  36.                 int = selectletterCase(sLine.Substring(i - 1, 1))
  37.                 lettercount(int) += 1
  38.             Next
  39.  
  40.             For x = 0 To 25
  41.                 letterArray(x).Text = lettercount(x).ToString
  42.                 MsgBox(letterArray(x).ToString)
  43.             Next
  44.         End If
  45.     End SubPrivate Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmit.Click

any help is appreciated!

Newbie
 
Join Date: Feb 2008
Posts: 15
#3: Feb 16 '08

re: I dont want objreader to read spaces or character


thanks for the help with the remove space problem

the problem now has something to do with my select case function. If int = selectletterCase(sLine.Substring(i - 1, 1))

is sent either an "a" , " space " or " any type of character " it returns 0 and will add 1 to the total count for "a"
Newbie
 
Join Date: Feb 2008
Posts: 15
#4: Feb 16 '08

re: I dont want objreader to read spaces or character


figured it out.

Need a Case Else that returns a null.
Reply