reading file and getting input

Posted 28 November 2009 - 08:30 AM

I am working on a project that needs to read a file, no prob I have that part but, how do I code it to read the file and get say the highest, lowest, average and sum of the numbers in the file? Everything I have tried didn't work and left me with a blank application.

Dim blah As String = File.ReadAllText("C:\filepath\file.txt")
Dim ttt() As String = blah.Split(";")
Dim meh(ttt.Length - 1) As Integer
Dim aaa As Integer
For aaa = 0 To ttt.Length - 1
meh(aaa) = ttt(aaa)
Next
MsgBox("highest = " & meh.Max)
MsgBox("lowest = " & meh.Min)
'look at the pattern and mess around with intellisense to see if you can figure out how to get the average :)

This post has been edited by LeisureProgrammer: 28 November 2009 - 08:58 AM

Re: reading file and getting input

Posted 28 November 2009 - 09:06 AM

Try something like this:

Dim ttt() As String = File.ReadAllLines("C:\filepath\file.txt")
Dim meh(ttt.Length - 1) As Double
Dim aaa As Integer
For aaa = 0 To ttt.Length - 1
meh(aaa) = ttt(aaa)
Next
MsgBox("highest = " & meh.Max)
MsgBox("lowest = " & meh.Min)
'look at the pattern and mess around with intellisense to see if you can figure out how to get the average - same with the sum :)

This post has been edited by LeisureProgrammer: 28 November 2009 - 09:12 AM

Re: reading file and getting input

Posted 28 November 2009 - 09:42 AM

I'm working on making a project for you to learn from at the moment. I plan on commenting the source so you get a understanding.

Edit- This should help.

Source

' :::::::::::::::::::::::::::::::::
' ::: Exile Studios :::
' ::: Created by Exile :::
' ::: http://exile-studios.info :::
' :::::::::::::::::::::::::::::::::
Imports System.IO
Public Class Form1
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' ::: Shows the open file dialog so the user and select a file to open :::
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ofd.ShowDialog()
End Sub
Private Sub ofd_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofd.FileOk
' :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' ::: Reads path of the file opened :::
' ::: Then reads all of the lines in the file into a string array :::
' :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Dim Numbers() As String = File.ReadAllLines(ofd.FileName)
Dim Average As Double
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' ::: Put the number of lines in the textbox that displays the total amount of numbers :::
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
txtTotal.Text = Numbers.Count()
' :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' ::: We loop through the number of lines to add up each number :::
' ::: The number of lines is subtracted by 1 because arrays begin at the index of 0 :::
' :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
For i = 0 To Numbers.Count - 1
Average += Numbers(i)
Next
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' ::: Since we've added all of the numbers up we need to divide it by the amount of numbers. :::
' ::: This is required to get the average of a number :::
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Average = (Average / Numbers.Count)
' ::::::::::::::::::::::::::::::::::::::::::
' ::: Display the average in the textbox :::
' ::::::::::::::::::::::::::::::::::::::::::
txtAverage.Text = Average
' :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' ::: Reads the highest number in the string array and places it in the textbox :::
' :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
txtMax.Text = Numbers.Max
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' ::: Reads the lowest number in the string array and places it in the textbox :::
' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
txtMin.Text = Numbers.Min
End Sub
End Class