console app help

hdavidson

New member
Joined
Jun 28, 2005
Messages
4
Programming Experience
Beginner
console app help/ RESOLVED

Hi everyone, I am new to vb.net and I am having problems getting a console application to read a text file into an array and perform various calculations on it after that. I have done everything I think I know how to do and cannot even get the program to read the file when I call my readinputfile function. Any help would be greatly appreciated. I am including my code that I have.
Module Module1

Sub Main()

Dim array AsInteger()

Dim i AsInteger

Dim avg AsInteger

Dim n AsString

array = NewInteger() {}

Console.WriteLine("Please enter the filename to be evaluated")

Console.ReadLine()

ReadInputFile(array, n)

For i = 0 To array.GetUpperBound(0)

array(i) = array(n)

Next

For i = 0 To array.GetUpperBound(0)

Console.WriteLine("Mean" & vbTab & Mean(array, n))

Console.WriteLine("Maximum" & vbTab & Maximum(array, n))

Console.WriteLine("Minimum" & vbTab & Minimum(array, n))

Console.WriteLine("Median" & vbTab & Median(array, n))

Console.WriteLine("Mode" & vbTab & Mode(array, n))

Next

Console.WriteLine("Press any key to continue")

Console.ReadLine()

EndSub

Function Mean(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger

'allocating first array and copying its reference

Dim sum AsInteger = 0

Dim i AsInteger

For i = 0 To n

sum += ar(i)

Next

Return sum / n

EndFunction

Function ReadInputFile(ByRef ar() AsInteger, ByVal filename AsString) AsInteger

Dim n AsInteger = 0

Try

Dim fs AsNew System.IO.StreamReader(filename)

Dim line AsString

Do

line = fs.ReadLine()

IfNot line IsNothingThen

ar(n) = Convert.ToInt16(line)

n += 1

EndIf

LoopUntil line IsNothing

fs.Close()

Return n

Catch E As Exception

' let the user know what went wrong

Console.WriteLine("The file could not be read:")

Console.WriteLine(E.Message)

EndTry

Return n 'number of elements read from file

EndFunction

Function Maximum(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger

' n = number of elements in array

Dim max AsInteger = ar(0)

Dim i AsInteger

For i = 1 To n

If (ar(i) > max) Then max = ar(i)

Next

Return max

EndFunction

Function Minimum(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger

' n = number of elements in array

Dim min AsInteger = ar(0)

Dim i AsInteger

For i = 1 To n

If (ar(i) < min) Then min = ar(i)

Next

Return min

EndFunction

Function Median(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger

'n = number of elements in array

Dim i AsInteger

Dim count AsInteger = ar.GetLength(0)

If count = 0 ThenReturn (0)

ar.Sort(ar)

For i = 0 To n

If count Mod 2 = 0 Then

Return (ar((count / 2) - 1) + ar((count / 2))) / 2

Else

Return ar(count / 2)

EndIf

Next

EndFunction

Function Mode(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger

Dim i AsInteger

Dim Count AsInteger

Dim Number() AsDecimal

Dim CountOfNumber AsInteger

Dim CurrentNumber AsDecimal

Dim Counter AsInteger

Dim HighestNumberIndex AsInteger

Dim HighestNumberCount AsInteger

Count = ar.GetUpperBound(0)

If Count = 0 ThenReturn 0

ar.Sort(ar)

ReDim Number(0)

CurrentNumber = ar(0)

HighestNumberIndex = 0

HighestNumberCount = 0

Number(0) = CurrentNumber

For i = 0 To n

While Counter <= Count

If CurrentNumber = ar(Counter) Then

CountOfNumber += 1

If CountOfNumber > HighestNumberCount Then

HighestNumberCount = CountOfNumber

HighestNumberIndex = Number.GetUpperBound(0)

EndIf

Else

ReDimPreserve Number(Number.GetUpperBound(0) + 1)

CurrentNumber = ar(Counter)

Number(Number.GetUpperBound(0)) = CurrentNumber

CountOfNumber = 1

EndIf

Counter += 1

EndWhile

Next

Return Number(HighestNumberIndex)

EndFunction

Function StdDev(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger

Dim i AsInteger

Dim avg AsInteger, SumSq AsInteger

avg = Mean(ar, n)

For i = 1 To n

SumSq = SumSq + (ar(i) - avg) ^ 2

Next i

Return StdDev = Sqr(SumSq / ((ar(0)) - 1))

EndFunction





End
Module

 
Last edited:

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Sorry, too much code for consideration but if you want to only read a text file ... below is the simpliest way to do that:

PHP:
 Module Module1 
 
Sub Main()
 
Dim strContents As String
 
Dim objReader As System.IO.StreamReader
 
objReader = New System.IO.StreamReader("C:\myFile.txt")
 
strContents = objReader.ReadToEnd()
 
objReader.Close()
 
Console.Write(strContents)
 
Console.WriteLine("Press Enter key to continue")
 
Console.Read()
 
End Sub
 
End Module

Cheers ;)


btw, welcome to the forum :)
 

hdavidson

New member
Joined
Jun 28, 2005
Messages
4
Programming Experience
Beginner
reply to help

I was wondering how to make the file input any random file, with the path included. I toyed with your sub and it is pasted below. Any suggestions?

Sub ReadInputFile()
Dim strContents As String
Dim objReader As System.IO.StreamReader
Dim n As String
objReader = New System.IO.StreamReader(n)
strContents = objReader.ReadToEnd()
objReader.Close()
Console.Write(strContents)
Console.WriteLine("Press Enter key to continue")
Console.Read()
End Sub


Additionally I was wondering if you have any information about how to get the data from the file into the array I am working with?

Thanks
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
I couldn't recall anything better at the moment ... i will but tomorow :(

For now i have this: If you add separator like semicolon ";" at the end of each line then we have a solution. For instance:

TextFile said:
Var1 = 12;
Var2 = 120;
Var3 = 100

PHP:
 Module Module1 
 
Sub Main()
 
Dim strContents As String
 
Dim objReader As System.IO.StreamReader
 
objReader = New System.IO.StreamReader("C:\myFile.txt")
 
Dim q As String = objReader.ReadToEnd()
 
Dim w() As String = q.Split(";")
 
Dim i As Integer
 
For i = 0 To w.GetUpperBound(0)
 
Console.Write(w(i))
 
Next
 
objReader.Close()
 
Console.WriteLine("Press Enter key to continue")
'now you can manipulate each string separately'
Console.Write(w(0)) 'here we call first line again'
 
Console.Write(w(1)) 'here we call second line again .... and so on ,,, you can distinct every line of the text file'
 
Console.WriteLine("Press Enter key to continue")
 
Console.Read()
 
End Sub
 
End Module



Cheers ;)
 
Last edited:

hdavidson

New member
Joined
Jun 28, 2005
Messages
4
Programming Experience
Beginner
how to read a text file into an array

In earlier posts I was having problems calling a function to read a text file into an array. Below is the function I used and how I called it. It works with any text file with integers, just include the path along with the name.

Sub Main()
dim ar() as integer
dim n as integer
dim filename as string

' prompt user to enter file name

Console.WriteLine("Please enter the filename to be evaluated:")

filename = Console.ReadLine()



' read the input file

n = ReadInputFile(ar, filename)


End Main
Function ReadInputFile(ByRef ar() As Integer, Optional ByVal filename As String = " ") As Integer

Dim n As Integer = 0

Try

Dim fs As New System.IO.StreamReader(filename)

Dim line As String

Do

line = fs.ReadLine()

If Not line Is Nothing Then

ar(n) = Convert.ToInt16(line)

n += 1

End If

Loop Until line Is Nothing

fs.Close()

Return n

Catch E As Exception

' let the user know what went wrong

Console.WriteLine("The file could not be read:")

Console.WriteLine(E.Message)

End Try

Return n

End Function

 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
just a suggestion that takes care of user error when dealing with file selection:

VB.NET:
Sub Main()
		Dim ar() As Integer
		Dim n As Integer
		Dim filename As String
		Dim ofdFile As New OpenFileDialog
		Dim dlgResult As DialogResult
		With ofdFile
			.Filter = "Text File (*.txt)|.txt"
		 .FileName = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\"
			dlgResult = .ShowDialog
		End With
		If dlgResult <> DialogResult.Cancel Then
			filename = ofdFile.FileName
			' prompt user to enter file name
			   'Console.WriteLine("Please enter the filename to be evaluated:")
			'filename = Console.ReadLine()
			' read the input file
			n = ReadInputFile(ar, filename)
		End If
	End Sub

	Function ReadInputFile(ByRef ar() As Integer, Optional ByVal filename As String = " ") As Integer
		Dim n As Integer = 0
		Try
			Dim fs As New System.IO.StreamReader(filename)
			Dim line As String
			Do
				line = fs.ReadLine()
				If Not line Is Nothing Then
				 ar(n) = Convert.ToInt16(line)
					n += 1
				End If
			Loop Until line Is Nothing
			fs.Close()
			Return n
		Catch E As Exception
			' let the user know what went wrong
			Console.WriteLine("The file could not be read:")
			Console.WriteLine(E.Message)
		End Try
		Return n
	End Function
 
Top Bottom