Solved

Count the number of Characters,words,sentences, and paragraphs in a file

Posted on 2004-04-21
19
576 Views
Last Modified: 2012-06-21
I need to Count the number of Characters,words,sentences, and paragraphs in a file that is opened in a text box, and output it to 4 textboxes named the same.
0
Comment
Question by:TexRedneck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
19 Comments
 
LVL 27

Expert Comment

by:planocz
ID: 10880693
This sure sounds like a home work problem!
0
 

Author Comment

by:TexRedneck
ID: 10880760
It might be but how else can I get help when it is not in the book. Any help will be better than nothing, I am a newbe to programming.
0
 
LVL 27

Expert Comment

by:planocz
ID: 10880810
does the file have to be inside a textbox?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:malharone
ID: 10880863
use regular expressions


for sentence:
Regex regex = new Regex(
    @"(?sx-m)[^\r\n].*?(?:(?:\.|\?|!)\s)     # Sentences",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


for words:
Dim regex = New Regex( _
    "\w*(?x)        # All words", _
    RegexOptions.IgnoreCase _
    Or RegexOptions.Multiline _
    Or RegexOptions.IgnorePatternWhitespace _
    Or RegexOptions.Compiled _
    )

characters:
your_string.length
0
 

Author Comment

by:TexRedneck
ID: 10880877
yes it uses an OpenFileDialoge control to view the chose the file to open, and then displayes it in the txtDisplay.text box, then it outputs the calculation in the txtChar, txtWords, txtSentences, and txtPara textboxes.

Thank you for any help you can give!
0
 
LVL 27

Expert Comment

by:planocz
ID: 10881145
Hi TexRedneck,

Experts-exchange members do not like doing "Home work", But here is a start for you
to work with; the rest is up to you to finish.
Just start a new project and paste this code into it.

Imports System.IO
Public Class frmReadaFile
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lblChars As System.Windows.Forms.Label
    Friend WithEvents lblWords As System.Windows.Forms.Label
    Friend WithEvents lblSentences As System.Windows.Forms.Label
    Friend WithEvents lblParagraphs As System.Windows.Forms.Label
    Friend WithEvents txtWords As System.Windows.Forms.TextBox
    Friend WithEvents txtSentences As System.Windows.Forms.TextBox
    Friend WithEvents txtParagraphs As System.Windows.Forms.TextBox
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents cmdFile As System.Windows.Forms.Button
    Friend WithEvents txtFile As System.Windows.Forms.TextBox
    Friend WithEvents txtChars As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblChars = New System.Windows.Forms.Label
        Me.lblWords = New System.Windows.Forms.Label
        Me.lblSentences = New System.Windows.Forms.Label
        Me.lblParagraphs = New System.Windows.Forms.Label
        Me.txtChars = New System.Windows.Forms.TextBox
        Me.txtWords = New System.Windows.Forms.TextBox
        Me.txtSentences = New System.Windows.Forms.TextBox
        Me.txtParagraphs = New System.Windows.Forms.TextBox
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.cmdFile = New System.Windows.Forms.Button
        Me.txtFile = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'lblChars
        '
        Me.lblChars.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblChars.Location = New System.Drawing.Point(12, 20)
        Me.lblChars.Name = "lblChars"
        Me.lblChars.Size = New System.Drawing.Size(44, 16)
        Me.lblChars.TabIndex = 0
        Me.lblChars.Text = "Char #"
        '
        'lblWords
        '
        Me.lblWords.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblWords.Location = New System.Drawing.Point(12, 48)
        Me.lblWords.Name = "lblWords"
        Me.lblWords.Size = New System.Drawing.Size(52, 16)
        Me.lblWords.TabIndex = 1
        Me.lblWords.Text = "Words #"
        '
        'lblSentences
        '
        Me.lblSentences.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSentences.Location = New System.Drawing.Point(12, 80)
        Me.lblSentences.Name = "lblSentences"
        Me.lblSentences.Size = New System.Drawing.Size(80, 16)
        Me.lblSentences.TabIndex = 2
        Me.lblSentences.Text = "Sentences #"
        '
        'lblParagraphs
        '
        Me.lblParagraphs.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblParagraphs.Location = New System.Drawing.Point(12, 108)
        Me.lblParagraphs.Name = "lblParagraphs"
        Me.lblParagraphs.Size = New System.Drawing.Size(80, 16)
        Me.lblParagraphs.TabIndex = 3
        Me.lblParagraphs.Text = "Paragraphs #"
        '
        'txtChars
        '
        Me.txtChars.Location = New System.Drawing.Point(116, 16)
        Me.txtChars.Name = "txtChars"
        Me.txtChars.Size = New System.Drawing.Size(88, 20)
        Me.txtChars.TabIndex = 4
        Me.txtChars.Text = ""
        '
        'txtWords
        '
        Me.txtWords.Location = New System.Drawing.Point(116, 44)
        Me.txtWords.Name = "txtWords"
        Me.txtWords.Size = New System.Drawing.Size(88, 20)
        Me.txtWords.TabIndex = 5
        Me.txtWords.Text = ""
        '
        'txtSentences
        '
        Me.txtSentences.Location = New System.Drawing.Point(116, 76)
        Me.txtSentences.Name = "txtSentences"
        Me.txtSentences.Size = New System.Drawing.Size(88, 20)
        Me.txtSentences.TabIndex = 6
        Me.txtSentences.Text = ""
        '
        'txtParagraphs
        '
        Me.txtParagraphs.Location = New System.Drawing.Point(116, 104)
        Me.txtParagraphs.Name = "txtParagraphs"
        Me.txtParagraphs.Size = New System.Drawing.Size(88, 20)
        Me.txtParagraphs.TabIndex = 7
        Me.txtParagraphs.Text = ""
        '
        'cmdFile
        '
        Me.cmdFile.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdFile.Location = New System.Drawing.Point(284, 16)
        Me.cmdFile.Name = "cmdFile"
        Me.cmdFile.Size = New System.Drawing.Size(96, 28)
        Me.cmdFile.TabIndex = 8
        Me.cmdFile.Text = "Get File"
        '
        'txtFile
        '
        Me.txtFile.Location = New System.Drawing.Point(228, 68)
        Me.txtFile.Multiline = True
        Me.txtFile.Name = "txtFile"
        Me.txtFile.Size = New System.Drawing.Size(208, 164)
        Me.txtFile.TabIndex = 9
        Me.txtFile.Text = "TextBox1"
        '
        'frmReadaFile
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(448, 266)
        Me.Controls.Add(Me.txtFile)
        Me.Controls.Add(Me.cmdFile)
        Me.Controls.Add(Me.txtParagraphs)
        Me.Controls.Add(Me.txtSentences)
        Me.Controls.Add(Me.txtWords)
        Me.Controls.Add(Me.txtChars)
        Me.Controls.Add(Me.lblParagraphs)
        Me.Controls.Add(Me.lblSentences)
        Me.Controls.Add(Me.lblWords)
        Me.Controls.Add(Me.lblChars)
        Me.Name = "frmReadaFile"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub cmdFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdFile.Click
        Dim myStream As Stream
        Dim openFileDialog1 As New OpenFileDialog

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            'Place into Textbox
            txtFile.Text = StreamReaderReadFromFile(openFileDialog1.FileName)

            'Re-read file to count chars
            StreamReaderReadINCHARS(openFileDialog1.FileName)

            myStream = openFileDialog1.OpenFile()
            If Not (myStream Is Nothing) Then
                ' Insert code to read the stream here.
                myStream.Close()
            End If
        End If
    End Sub
    ' This subrouting uses a StreamReader object to open an existing file
    '   and read it character by character. It then outputs each character
    '   after a short pause, to show the user that the file is being read
    '   by characters. The output is added to the txtChars text box.
    Private Sub StreamReaderReadINCHARS(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim myNextInt As Integer
        Dim iCounter As Short

        Try
            ' Create a StreamReader using a Shared (static) File class.
            myStreamReader = File.OpenText(sFileName)

            ' The Read() method returns an integer.
            myNextInt = myStreamReader.Read()
            ' The Read() method returns '-1' when the end of the
            '   file has been reached
            While myNextInt <> -1
                ' Convert the integer to a unicode Char and add it
                '   to sData var.
                iCounter += 1
                txtChars.Text = iCounter
                ' Read the next value from the Stream
                myNextInt = myStreamReader.Read()
            End While
        Catch exc As Exception
            MsgBox("File could not be opened or read." + vbCrLf + _
                "Please verify that the filename is correct, " + _
                "and that you have read permissions for the desired " + _
                "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
        End Try
    End Sub
    ' This subrouting uses a StreamReader object to open an existing file
    '   and read it in one pass and place the text in the txtFile text box.
    Public Function StreamReaderReadFromFile(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim sData As String

        Try
            myStreamReader = File.OpenText(sFileName)
            ' Read the entire file in one pass, and add the contents to sData var.
            sData = myStreamReader.ReadToEnd()
        Catch exc As Exception
            MsgBox("File could not be opened or read." + vbCrLf + _
                "Please verify that the filename is correct, " + _
                "and that you have read permissions for the desired " + _
                "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
            StreamReaderReadFromFile = sData
        End Try
    End Function
End Class
0
 
LVL 9

Expert Comment

by:malharone
ID: 10881200
   Private Sub summarize(ByVal text As String)
        Dim reg As Regex
        Dim r As String
        r = "(?sx-m)[^\r\n].*?(?:(?:\.|\?|!)\s?)"
        reg = New Regex(r, RegexOptions.IgnoreCase Or RegexOptions.Multiline Or RegexOptions.IgnorePatternWhitespace Or RegexOptions.Compiled)

        MsgBox("Sentences: " & reg.Matches(text).Count)

        reg = New Regex("[A-z]+[^A-z]", RegexOptions.IgnoreCase Or RegexOptions.Multiline Or RegexOptions.IgnorePatternWhitespace Or RegexOptions.Compiled)
        MsgBox("words: " & reg.Matches(text).Count)

        MsgBox("characters: " & text.Replace(" ", "").Length)

    End Sub
0
 

Author Comment

by:TexRedneck
ID: 10881837
It dosen't display the file in the textbox? just the chars count
0
 
LVL 27

Expert Comment

by:planocz
ID: 10881904
it works here.

you will need to place a break point in the cmdFile_Click to watch what the code is doing.
0
 
LVL 9

Expert Comment

by:malharone
ID: 10881905
HUH???

if you're using vb.net .. then you want to call the function with the right param.
After that, the program will popup 3 boxes:
1 for # of sentences
1 for # of words
and 1 for # of chars
0
 

Author Comment

by:TexRedneck
ID: 10902385
Plaese Help this is my last week doing vb.net and i am stuck!!!!

Imports System.IO
Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code txtDisplay.
    Friend WithEvents lblChars As System.Windows.Forms.Label
    Friend WithEvents lblWords As System.Windows.Forms.Label
    Friend WithEvents lblSentences As System.Windows.Forms.Label
    Friend WithEvents lblParagraphs As System.Windows.Forms.Label
    Friend WithEvents txtWords As System.Windows.Forms.TextBox
    Friend WithEvents txtSentences As System.Windows.Forms.TextBox
    Friend WithEvents txtParagraphs As System.Windows.Forms.TextBox
    Friend WithEvents cmdFile As System.Windows.Forms.Button
    Friend WithEvents txtChars As System.Windows.Forms.TextBox
    Friend WithEvents ofdFile As System.Windows.Forms.OpenFileDialog
    Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
    Friend WithEvents cmdText As System.Windows.Forms.Button
    Friend WithEvents cmdExit As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblChars = New System.Windows.Forms.Label
        Me.lblWords = New System.Windows.Forms.Label
        Me.lblSentences = New System.Windows.Forms.Label
        Me.lblParagraphs = New System.Windows.Forms.Label
        Me.txtChars = New System.Windows.Forms.TextBox
        Me.txtWords = New System.Windows.Forms.TextBox
        Me.txtSentences = New System.Windows.Forms.TextBox
        Me.txtParagraphs = New System.Windows.Forms.TextBox
        Me.ofdFile = New System.Windows.Forms.OpenFileDialog
        Me.cmdFile = New System.Windows.Forms.Button
        Me.txtDisplay = New System.Windows.Forms.TextBox
        Me.cmdText = New System.Windows.Forms.Button
        Me.cmdExit = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'lblChars
        '
        Me.lblChars.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblChars.Location = New System.Drawing.Point(162, 18)
        Me.lblChars.Name = "lblChars"
        Me.lblChars.Size = New System.Drawing.Size(90, 16)
        Me.lblChars.TabIndex = 0
        Me.lblChars.Text = "Char #"
        '
        'lblWords
        '
        Me.lblWords.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblWords.Location = New System.Drawing.Point(162, 42)
        Me.lblWords.Name = "lblWords"
        Me.lblWords.Size = New System.Drawing.Size(90, 16)
        Me.lblWords.TabIndex = 1
        Me.lblWords.Text = "Words #"
        '
        'lblSentences
        '
        Me.lblSentences.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSentences.Location = New System.Drawing.Point(162, 78)
        Me.lblSentences.Name = "lblSentences"
        Me.lblSentences.Size = New System.Drawing.Size(84, 16)
        Me.lblSentences.TabIndex = 2
        Me.lblSentences.Text = "Sentences #"
        '
        'lblParagraphs
        '
        Me.lblParagraphs.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblParagraphs.Location = New System.Drawing.Point(162, 102)
        Me.lblParagraphs.Name = "lblParagraphs"
        Me.lblParagraphs.Size = New System.Drawing.Size(80, 16)
        Me.lblParagraphs.TabIndex = 3
        Me.lblParagraphs.Text = "Paragraphs #"
        '
        'txtChars
        '
        Me.txtChars.Location = New System.Drawing.Point(264, 12)
        Me.txtChars.Name = "txtChars"
        Me.txtChars.Size = New System.Drawing.Size(88, 20)
        Me.txtChars.TabIndex = 4
        Me.txtChars.Text = ""
        '
        'txtWords
        '
        Me.txtWords.Location = New System.Drawing.Point(264, 42)
        Me.txtWords.Name = "txtWords"
        Me.txtWords.Size = New System.Drawing.Size(88, 20)
        Me.txtWords.TabIndex = 5
        Me.txtWords.Text = ""
        '
        'txtSentences
        '
        Me.txtSentences.Location = New System.Drawing.Point(264, 72)
        Me.txtSentences.Name = "txtSentences"
        Me.txtSentences.Size = New System.Drawing.Size(88, 20)
        Me.txtSentences.TabIndex = 6
        Me.txtSentences.Text = ""
        '
        'txtParagraphs
        '
        Me.txtParagraphs.Location = New System.Drawing.Point(264, 102)
        Me.txtParagraphs.Name = "txtParagraphs"
        Me.txtParagraphs.Size = New System.Drawing.Size(88, 20)
        Me.txtParagraphs.TabIndex = 7
        Me.txtParagraphs.Text = ""
        '
        'cmdFile
        '
        Me.cmdFile.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdFile.Location = New System.Drawing.Point(186, 312)
        Me.cmdFile.Name = "cmdFile"
        Me.cmdFile.Size = New System.Drawing.Size(96, 28)
        Me.cmdFile.TabIndex = 8
        Me.cmdFile.Text = "Analyze File"
        '
        'txtDisplay
        '
        Me.txtDisplay.BackColor = System.Drawing.Color.White
        Me.txtDisplay.Location = New System.Drawing.Point(12, 132)
        Me.txtDisplay.Multiline = True
        Me.txtDisplay.Name = "txtDisplay"
        Me.txtDisplay.ReadOnly = True
        Me.txtDisplay.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal
        Me.txtDisplay.Size = New System.Drawing.Size(516, 164)
        Me.txtDisplay.TabIndex = 9
        Me.txtDisplay.Text = ""
        '
        'cmdText
        '
        Me.cmdText.Location = New System.Drawing.Point(312, 312)
        Me.cmdText.Name = "cmdText"
        Me.cmdText.Size = New System.Drawing.Size(78, 24)
        Me.cmdText.TabIndex = 10
        Me.cmdText.Text = "Analyze Text"
        '
        'cmdExit
        '
        Me.cmdExit.Location = New System.Drawing.Point(432, 312)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.TabIndex = 11
        Me.cmdExit.Text = "Exit"
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(538, 351)
        Me.Controls.Add(Me.cmdExit)
        Me.Controls.Add(Me.cmdText)
        Me.Controls.Add(Me.txtDisplay)
        Me.Controls.Add(Me.cmdFile)
        Me.Controls.Add(Me.txtParagraphs)
        Me.Controls.Add(Me.txtSentences)
        Me.Controls.Add(Me.txtWords)
        Me.Controls.Add(Me.txtChars)
        Me.Controls.Add(Me.lblParagraphs)
        Me.Controls.Add(Me.lblSentences)
        Me.Controls.Add(Me.lblWords)
        Me.Controls.Add(Me.lblChars)
        Me.Name = "frmMain"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
   

    Private Sub cmdFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdFile.Click
        Dim myStream As Stream
        Dim openFileDialog1 As New OpenFileDialog

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            'Place into Textbox
            txtDisplay.Text = StreamReaderReadFromFile(openFileDialog1.FileName)
        End If
    End Sub

    Private Sub cmdText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdText.Click
        Dim myStreamReader As StreamReader
        Dim myNextInt As Integer
        Dim iCounter As Short
        Dim sFileName As String
        Dim openFileDialog1 As New OpenFileDialog
        ' This subrouting uses a StreamReader object to open an existing file
        '   and read it character by character. It then outputs each character
        '   after a short pause, to show the user that the file is being read
        '   by characters. The output is added to the txtChars text box.


        Try
            ' Create a StreamReader using a Shared (static) File class.

            StreamReaderReadFromFile(openFileDialog1.FileName)
            ' The Read() method returns an integer.
            myNextInt = myStreamReader.Read()
            ' The Read() method returns '-1' when the end of the
            '   file has been reached
            While myNextInt <> -1
                ' Convert the integer to a unicode Char and add it
                '   to sData var.
                iCounter += 1
                txtChars.Text = iCounter
                ' Read the next value from the Stream
                myNextInt = myStreamReader.Read()
            End While
        Catch exc As Exception
            MsgBox("File could not be opened or read.")
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
        End Try
     
    End Sub

    Private Sub StreamReaderReadINChars(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim myNextInt As Integer
        Dim iCounter As Short
        Dim myStream As Stream
        Dim openFileDialog1 As New OpenFileDialog


        'Re-read file to count chars
        StreamReaderReadINChars(openFileDialog1.FileName)
        myStream = openFileDialog1.OpenFile()
       
    End Sub
    Public Function StreamReaderReadFromFile(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim sData As String

        Try
            myStreamReader = File.OpenText(sFileName)
            ' Read the entire file in one pass, and add the contents to sData var.
            sData = myStreamReader.ReadToEnd()
        Catch exc As Exception
            MsgBox("File could not be opened or read.")
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
            StreamReaderReadFromFile = sData
        End Try
    End Function


End Class
0
 
LVL 27

Expert Comment

by:planocz
ID: 10902469
what are you stuck on?
0
 

Author Comment

by:TexRedneck
ID: 10902578
did you run the Code and see the problem, i need to open the file with one cmdbtn, and get the chars,words, sen, and per with another cmdbtn.

Help
0
 
LVL 9

Expert Comment

by:malharone
ID: 10902669
change:
    Public Function StreamReaderReadFromFile(ByVal sFileName As String) as string '<--- change
        Dim myStreamReader As StreamReader
        Dim sData As String

        Try
            myStreamReader = File.OpenText(sFileName)
            ' Read the entire file in one pass, and add the contents to sData var.
            sData = myStreamReader.ReadToEnd()
        Catch exc As Exception
            MsgBox("File could not be opened or read.")
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
        End Try
        return sData            '<--- return line moved
    End Function


once you do that then you should use the "summarize" function I provided and pass the contents of the file as a parameter.
0
 

Author Comment

by:TexRedneck
ID: 10902746
I am tring to go back and forth between both of you and now im realy confused.
0
 
LVL 27

Accepted Solution

by:
planocz earned 500 total points
ID: 10903247
Try this code....
I have to leave work so it will be tomorrow before I can get back with you.

Imports System.IO
Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lblChars As System.Windows.Forms.Label
    Friend WithEvents lblWords As System.Windows.Forms.Label
    Friend WithEvents lblSentences As System.Windows.Forms.Label
    Friend WithEvents lblParagraphs As System.Windows.Forms.Label
    Friend WithEvents txtWords As System.Windows.Forms.TextBox
    Friend WithEvents txtSentences As System.Windows.Forms.TextBox
    Friend WithEvents txtParagraphs As System.Windows.Forms.TextBox
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents cmdFile As System.Windows.Forms.Button
    Friend WithEvents txtFile As System.Windows.Forms.TextBox
    Friend WithEvents txtChars As System.Windows.Forms.TextBox
    Friend WithEvents cmdExit As System.Windows.Forms.Button
    Friend WithEvents txtLength As System.Windows.Forms.TextBox
    Friend WithEvents lblTotal As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblChars = New System.Windows.Forms.Label
        Me.lblWords = New System.Windows.Forms.Label
        Me.lblSentences = New System.Windows.Forms.Label
        Me.lblParagraphs = New System.Windows.Forms.Label
        Me.txtChars = New System.Windows.Forms.TextBox
        Me.txtWords = New System.Windows.Forms.TextBox
        Me.txtSentences = New System.Windows.Forms.TextBox
        Me.txtParagraphs = New System.Windows.Forms.TextBox
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.cmdFile = New System.Windows.Forms.Button
        Me.txtFile = New System.Windows.Forms.TextBox
        Me.cmdExit = New System.Windows.Forms.Button
        Me.txtLength = New System.Windows.Forms.TextBox
        Me.lblTotal = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'lblChars
        '
        Me.lblChars.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblChars.Location = New System.Drawing.Point(12, 20)
        Me.lblChars.Name = "lblChars"
        Me.lblChars.Size = New System.Drawing.Size(44, 16)
        Me.lblChars.TabIndex = 0
        Me.lblChars.Text = "Char #"
        '
        'lblWords
        '
        Me.lblWords.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblWords.Location = New System.Drawing.Point(12, 48)
        Me.lblWords.Name = "lblWords"
        Me.lblWords.Size = New System.Drawing.Size(52, 16)
        Me.lblWords.TabIndex = 1
        Me.lblWords.Text = "Words #"
        '
        'lblSentences
        '
        Me.lblSentences.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSentences.Location = New System.Drawing.Point(184, 20)
        Me.lblSentences.Name = "lblSentences"
        Me.lblSentences.Size = New System.Drawing.Size(80, 16)
        Me.lblSentences.TabIndex = 2
        Me.lblSentences.Text = "Sentences #"
        '
        'lblParagraphs
        '
        Me.lblParagraphs.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblParagraphs.Location = New System.Drawing.Point(184, 48)
        Me.lblParagraphs.Name = "lblParagraphs"
        Me.lblParagraphs.Size = New System.Drawing.Size(80, 16)
        Me.lblParagraphs.TabIndex = 3
        Me.lblParagraphs.Text = "Paragraphs #"
        '
        'txtChars
        '
        Me.txtChars.Location = New System.Drawing.Point(72, 16)
        Me.txtChars.Name = "txtChars"
        Me.txtChars.Size = New System.Drawing.Size(88, 20)
        Me.txtChars.TabIndex = 4
        Me.txtChars.Text = ""
        '
        'txtWords
        '
        Me.txtWords.Location = New System.Drawing.Point(72, 44)
        Me.txtWords.Name = "txtWords"
        Me.txtWords.Size = New System.Drawing.Size(88, 20)
        Me.txtWords.TabIndex = 5
        Me.txtWords.Text = ""
        '
        'txtSentences
        '
        Me.txtSentences.Location = New System.Drawing.Point(288, 16)
        Me.txtSentences.Name = "txtSentences"
        Me.txtSentences.Size = New System.Drawing.Size(88, 20)
        Me.txtSentences.TabIndex = 6
        Me.txtSentences.Text = ""
        '
        'txtParagraphs
        '
        Me.txtParagraphs.Location = New System.Drawing.Point(288, 44)
        Me.txtParagraphs.Name = "txtParagraphs"
        Me.txtParagraphs.Size = New System.Drawing.Size(88, 20)
        Me.txtParagraphs.TabIndex = 7
        Me.txtParagraphs.Text = ""
        '
        'cmdFile
        '
        Me.cmdFile.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdFile.Location = New System.Drawing.Point(484, 20)
        Me.cmdFile.Name = "cmdFile"
        Me.cmdFile.Size = New System.Drawing.Size(96, 28)
        Me.cmdFile.TabIndex = 8
        Me.cmdFile.Text = "Analyze &File"
        '
        'txtFile
        '
        Me.txtFile.Location = New System.Drawing.Point(12, 136)
        Me.txtFile.Multiline = True
        Me.txtFile.Name = "txtFile"
        Me.txtFile.Size = New System.Drawing.Size(560, 164)
        Me.txtFile.TabIndex = 9
        Me.txtFile.Text = "TextBox1"
        '
        'cmdExit
        '
        Me.cmdExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdExit.Location = New System.Drawing.Point(484, 60)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.Size = New System.Drawing.Size(96, 28)
        Me.cmdExit.TabIndex = 10
        Me.cmdExit.Text = "&Exit"
        '
        'txtLength
        '
        Me.txtLength.Location = New System.Drawing.Point(116, 76)
        Me.txtLength.Name = "txtLength"
        Me.txtLength.Size = New System.Drawing.Size(44, 20)
        Me.txtLength.TabIndex = 15
        Me.txtLength.Text = ""
        '
        'lblTotal
        '
        Me.lblTotal.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblTotal.Location = New System.Drawing.Point(4, 80)
        Me.lblTotal.Name = "lblTotal"
        Me.lblTotal.Size = New System.Drawing.Size(100, 16)
        Me.lblTotal.TabIndex = 14
        Me.lblTotal.Text = "Total File Length"
        '
        'frmReadaFile
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(592, 314)
        Me.Controls.Add(Me.txtLength)
        Me.Controls.Add(Me.lblTotal)
        Me.Controls.Add(Me.cmdExit)
        Me.Controls.Add(Me.txtFile)
        Me.Controls.Add(Me.cmdFile)
        Me.Controls.Add(Me.txtParagraphs)
        Me.Controls.Add(Me.txtSentences)
        Me.Controls.Add(Me.txtWords)
        Me.Controls.Add(Me.txtChars)
        Me.Controls.Add(Me.lblParagraphs)
        Me.Controls.Add(Me.lblSentences)
        Me.Controls.Add(Me.lblWords)
        Me.Controls.Add(Me.lblChars)
        Me.Name = "frmReadaFile"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub cmdFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdFile.Click
        Dim openFileDialog1 As New OpenFileDialog

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            'Place into Textbox
            txtFile.Text = StreamReaderReadFromFile(openFileDialog1.FileName)

            'Re-read file to count chars
            StreamReaderReadINCHARS(openFileDialog1.FileName)
        End If
    End Sub
    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
    ' This subrouting uses a StreamReader object to open an existing file
    '   and read it character by character. It then outputs each character
    '   after a short pause, to show the user that the file is being read
    '   by characters. The output is added to the txtChars text box.
    Private Sub StreamReaderReadINCHARS(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim myNextInt As Integer
        Dim iChar As Short
        Dim iWord As Short = 1
        Dim iPar As Short
        Dim iSentences As Short
        Dim iMisc As Short

        Try
            ' Create a StreamReader using a Shared (static) File class.
            myStreamReader = File.OpenText(sFileName)

            ' The Read() method returns an integer Decimal code for Char.
            myNextInt = myStreamReader.Read()
            ' The Read() method returns '-1' when the end of the
            '   file has been reached
            While myNextInt <> -1
                Select Case myNextInt
                    Case 9                   'tab sapcing for Paragraphs
                        iPar += 1
                    Case 32                   'spaces
                        iWord += 1
                    Case 44                   'commas
                        iMisc += 1
                    Case 46                   'period
                        iSentences += 1
                    Case 48 To 57             'numbers
                        iChar += 1
                    Case 65 To 90, 97 To 122  'Letters
                        iChar += 1
                End Select
                'iCounter += 1
                'txtChars.Text = iCounter
                ' Read the next value from the Stream
                myNextInt = myStreamReader.Read()
            End While
        Catch exc As Exception
            MsgBox("File could not be opened or read." + vbCrLf + _
                "Please verify that the filename is correct, " + _
                "and that you have read permissions for the desired " + _
                "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
            txtWords.Text = iWord.ToString
            txtChars.Text = iChar.ToString
            txtSentences.Text = iSentences.ToString
            txtParagraphs.Text = iPar.ToString
            txtLength.Text = (iChar + iWord + iSentences + iMisc + iPar).ToString
        End Try
    End Sub
    ' This subrouting uses a StreamReader object to open an existing file
    '   and read it in one pass and place the text in the txtFile text box.
    Public Function StreamReaderReadFromFile(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim sData As String

        Try
            myStreamReader = File.OpenText(sFileName)
            ' Read the entire file in one pass, and add the contents to sData var.
            sData = myStreamReader.ReadToEnd()
        Catch exc As Exception
            MsgBox("File could not be opened or read." + vbCrLf + _
                "Please verify that the filename is correct, " + _
                "and that you have read permissions for the desired " + _
                "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
        Finally
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
                myStreamReader.Close()
            End If
            StreamReaderReadFromFile = sData
        End Try
    End Function
End Class
0
 

Author Comment

by:TexRedneck
ID: 10903266
Malharone can you what order to use your code in?

please help.
0
 

Author Comment

by:TexRedneck
ID: 10903470
Thank you so muchit works great except for paragraphs, and i need a seperate button to do the actual calculations Analyze Text.
Analyze File just brings up the file to be analyzed and displayes it in the text box.

if you dont want to do the changes I understand I will turn it in like it is.
Thanks again for all the help.
TexRedneck
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS 2016 Rendering HTML tables 3 56
Asp.Net Session Question 2 33
Create a button class for use in all vb.net apps 5 14
Set arraylist as datatable 2 6
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question