Results 1 to 5 of 5

Thread: Totalling text file contents

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    3

    Totalling text file contents

    Please bare with me as I am a newbie trying to learn VS.

    I have a text file that contains 10 integers in a list format
    21
    15
    11
    9
    61
    45
    36
    97
    84
    11

    I wish to create an application that will:

    A- Display the list
    B- Allow editing of the list
    C- write the edits to the file (display error message if not integer)
    D- Display a message box that displays the sum of the integers is the list
    E- Re-Total the integers in the list automatically as edited in the text file display.

    I hope I described this accurately enough.
    thanks and any help would be appreciated.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Totalling text file contents

    There is no question, you are just describing what you want done. What do you have so far? What are you struggling with? What parts don't you understand?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    3

    Re: Totalling text file contents

    Public Class form1

    Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
    Dim FILE_NAME As String = "C:numbers.txt"
    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    TextBox1.Text = objReader.ReadToEnd
    objReader.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show("The Total Is", "Calculator")
    End Sub
    End Class

    My primary question is how do I display the sum of (numbers.txt) in a message box?

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Totalling text file contents

    You will have to loop through each line in the file and add the number to a variable. You will have to validate that each line is a number and actually can be cast to a numeric type. If you are only dealing with Integers then use Integer.TryParse on each line to make sure you don't try to add a non-numeric to your sum because that would result in an exception.

    An easier way to read the file is like this:

    vb.net Code:
    1. Dim fileName As String = "C:\numbers.txt"
    2. Dim fileContents() As String = IO.File.ReadAllLines(fileName)

    Perform your loops and validation on the fileContents array.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    3

    Re: Totalling text file contents

    Thanks for the input, it was a great help. Final output was the following and works fine. Thanks again.

    VB Code Code:
    1. Public Class form1
    2.     Const vbNewLine As String = vbCrLf
    3.  
    4.  
    5.    Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
    6.         Dim FILE_NAME As String = "C:\Documents and Settings\Doug\My Documents\computerDegreeInfo\computerProgramming-ITCS1130\Week 10\numbers.txt"
    7.         Dim objReader As New System.IO.StreamReader(FILE_NAME)
    8.         TextBox1.Text = objReader.ReadToEnd
    9.         objReader.Close()
    10.  
    11.         Dim lines As Integer = TextBox1.Lines.Length
    12.         Dim lineAdd(lines) As Integer
    13.         Dim counter As Integer = 0
    14.         Dim index As Integer = 0
    15.         Dim numbers As Integer = 0
    16.         Dim total As Integer = 0
    17.  
    18.         For counter = 0 To lines - 1
    19.  
    20.             If IsNumeric(TextBox1.Lines(index)) Then
    21.                 numbers = Convert.ToInt32(TextBox1.Lines(index))
    22.                 total += numbers
    23.                 index = index + 1
    24.             End If
    25.         Next
    26.         Label1.Text = " Your total is" & vbNewLine & total.ToString
    27.     End Sub
    28.  
    29.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    30.         Close()
    31.  
    32.     End Sub
    33.  
    34.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    35.         Dim lines As Integer = TextBox1.Lines.Length
    36.         Dim lineAdd(lines) As Integer
    37.         Dim counter As Integer = 0
    38.         Dim index As Integer = 0
    39.         Dim numbers As Integer = 0
    40.         Dim total As Integer = 0
    41.  
    42.         For counter = 0 To lines - 1
    43.  
    44.             If IsNumeric(TextBox1.Lines(index)) Then
    45.                 numbers = Convert.ToInt32(TextBox1.Lines(index))
    46.                 total += numbers
    47.                 index = index + 1
    48.             End If
    49.         Next
    50.         Label1.Text = " Your total is" & vbNewLine & total.ToString
    51.     End Sub
    52. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width