8 Replies - 832 Views - Last Post: 28 November 2009 - 09:42 AM Rate Topic: -----

#1 dee_dee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-October 09

reading file and getting input

Post icon  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.
Is This A Good Question/Topic? 0
  • +

Replies To: reading file and getting input

#2 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: reading file and getting input

Posted 28 November 2009 - 08:40 AM

Are the numbers separated by a character/line? If so, then it's easy:

For example, are the numbers in the file like this?

Quote

1;53;543;123;5;7;6;4;2;82


Supposing they are:

		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

Was This Post Helpful? 1
  • +
  • -

#3 karimi  Icon User is offline

  • Banned
  • member icon

Reputation: -9
  • View blog
  • Posts: 104
  • Joined: 29-May 09

Re: reading file and getting input

Posted 28 November 2009 - 08:52 AM

what type of file is it, CSV or the numbers are mixed with text. send a clear question
Was This Post Helpful? 0
  • +
  • -

#4 dee_dee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-October 09

Re: reading file and getting input

Posted 28 November 2009 - 09:02 AM

It's a text file and the numbers just go down the page.

The numbers are listed like this:

545.8267
188.4615
102.7812
270.6497
722.0612
440.2756
255.7861
336.2933
201.1559
224.9152
486.0958
142.6786
540.0567
184.0429

There are a 100 of them
Was This Post Helpful? 0
  • +
  • -

#5 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

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

Was This Post Helpful? 1
  • +
  • -

#6 dee_dee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-October 09

Re: reading file and getting input

Posted 28 November 2009 - 09:18 AM

Posted Image

That is what my application looks like so the msgbox codes wont work. :(
Was This Post Helpful? 0
  • +
  • -

#7 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: reading file and getting input

Posted 28 November 2009 - 09:20 AM

LOL, I'm sure you can figure that out in a few minutes - play around, and come back if you still don't get it. You do want to learn, don't you?

HINT:
Textbox1.text = "2"


Will put '2' in textbox1

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

Was This Post Helpful? 1
  • +
  • -

#8 dee_dee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-October 09

Re: reading file and getting input

Posted 28 November 2009 - 09:25 AM

Oh yes I do want to learn just wish the book was more informational! grr k gonna mess around with it will be back if I can't get it to work right thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#9 Deathx  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 23-November 08

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



Screenshot
Posted Image

Downloads
Source
Binary

Credits
Exile Studio's
Exile(Me) - Deathx is my old username and it remains my username on here.

This post has been edited by Deathx: 28 November 2009 - 10:23 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1