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.
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?
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?
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:
Dim fileName As String = "C:\numbers.txt"
Dim fileContents() As String = IO.File.ReadAllLines(fileName)
Perform your loops and validation on the fileContents array.
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:
Public Class form1
Const vbNewLine As String = vbCrLf
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
Dim FILE_NAME As String = "C:\Documents and Settings\Doug\My Documents\computerDegreeInfo\computerProgramming-ITCS1130\Week 10\numbers.txt"
Dim objReader As New System.IO.StreamReader(FILE_NAME)
TextBox1.Text = objReader.ReadToEnd
objReader.Close()
Dim lines As Integer = TextBox1.Lines.Length
Dim lineAdd(lines) As Integer
Dim counter As Integer = 0
Dim index As Integer = 0
Dim numbers As Integer = 0
Dim total As Integer = 0
For counter = 0 To lines - 1
If IsNumeric(TextBox1.Lines(index)) Then
numbers = Convert.ToInt32(TextBox1.Lines(index))
total += numbers
index = index + 1
End If
Next
Label1.Text = " Your total is" & vbNewLine & total.ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim lines As Integer = TextBox1.Lines.Length
Dim lineAdd(lines) As Integer
Dim counter As Integer = 0
Dim index As Integer = 0
Dim numbers As Integer = 0
Dim total As Integer = 0
For counter = 0 To lines - 1
If IsNumeric(TextBox1.Lines(index)) Then
numbers = Convert.ToInt32(TextBox1.Lines(index))
total += numbers
index = index + 1
End If
Next
Label1.Text = " Your total is" & vbNewLine & total.ToString
End Sub
End Class