Jump to content

ArrayList On One Line


iyeru42

Recommended Posts

I'm trying to get the output to be all on one line for each entry. However, currently it is coded to put each part of an entry on a new line.

Structure Student		Dim firstName As String		Dim lastName As String		Dim phoneNum As String		Dim examAvg As Double		Dim examGrade As String	End Structure	Dim anStudent As Student	Dim studentInfo As New ArrayList	Dim index As Integer	Sub Main()		readFiles()		writeData()		Console.ReadLine()	End Sub	Sub readFiles()		Dim stuFile As New StreamReader("..\student.txt")		Dim tempArray() As String		Do Until stuFile.Peek = -1			tempArray = Split(stuFile.ReadLine, ",")			anStudent.firstName = tempArray(0)			anStudent.lastName = tempArray(1)			anStudent.phoneNum = tempArray(2)			anStudent.examAvg = Convert.ToDouble(tempArray(3))			If (anStudent.examAvg > 89.9) Then				anStudent.examGrade = "A"			ElseIf (anStudent.examAvg > 79.9 AndAlso anStudent.examAvg < 89.9) Then				anStudent.examGrade = "B"			ElseIf (anStudent.examAvg > 69.9 AndAlso anStudent.examAvg < 79.9) Then				anStudent.examGrade = "C"			ElseIf (anStudent.examAvg > 59.9 AndAlso anStudent.examAvg < 69.9) Then				anStudent.examGrade = "D"			ElseIf (anStudent.examAvg < 59.9) Then				anStudent.examGrade = "F"			End If			For index = 0 To tempArray.Length - 1				studentInfo.Add(tempArray(index))			Next			studentInfo.Add(anStudent.examGrade)		Loop	End Sub	Sub writeData()		Console.WriteLine("Student Grade Report: 11/29/2006")		Console.WriteLine("How to read this screen:")		Console.WriteLine("Name  :  Grade Average  :  Grade")		Console.WriteLine("")		For Each student As String In studentInfo			Console.WriteLine(student)		Next	End Sub

Example of what it outputs currently:

J.Johnson123-123491.2A

And an example of how I WANT it to output:

J.Johnson	   91.2	AB

I also need the If/Else statement added for AB.

Link to comment
Share on other sites

For Each student As String In studentInfo			Console.WriteLine(student)		Next

See MSDN: Console.WriteLine(object):

Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.
You're telling your program to print your student object, but it doesn't know how to format each field in the object. By default, it prints it out in serialized format, which means you have a line break after each field.Its better to use .Net's formatting to take care of the alignment of text for you. I recommend rewriting your Sub like this:
Sub writeData()		Console.WriteLine("Student Grade Report: 11/29/2006")		Console.WriteLine("How to read this screen:")		Console.Write("{0,-20}{1,-20}{2,-20}", "Name:", "Grade Average:", "Grade")		Console.WriteLine("")		For Each student In studentInfo   'don't cast student as String			Console.WriteLine("{0,-20}{1,-20}{2,-20}", _					student.firstName & " " & student.LastName, _					student.examAverage, _					student.examGrade)		Next	End Sub

Link to comment
Share on other sites

Yeah but it now says...Student is a type and cannot be used as an expression.by using

Sub writeData()		Console.WriteLine("Student Grade Report: 11/29/2006")		Console.WriteLine("How to read this screen:")		Console.Write("{0,-20}{1,-20}{2,-20}", "Name:", "Grade Average:", "Grade")		Console.WriteLine("")		For Each student In studentInfo   'don't cast student as String			Console.WriteLine("{0,-20}{1,-20}{2,-20}", _					student.firstName & " " & student.LastName, _					student.examAverage, _					student.examGrade)		Next	End Sub

Link to comment
Share on other sites

In C# it'd be:

foreach(Student student in studentInfo){	...}

So I'm guessing that in VB it'd be:

For Each student As Student In studentInfo...Next

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...