4 Replies - 50446 Views - Last Post: 26 March 2008 - 09:39 AM Rate Topic: -----

#1 Shyne151  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-January 08

Reading CSV file into array (Visual Studio 2005)

Posted 21 January 2008 - 11:41 AM

A little bit too much php and c++ I suppose, but I can't remember VB for the life of me... and the prof isn't really feeling like refreshing my memory at all.

Writing a simple program that takes input from a CSV file... displays it and checks letter grade and computes numerical grade.

input sample
John,100,B
Sue,200,B
Joe,300,A
Tome,400,D






Public Class Form1
	Dim path As String = "data.txt"
	Dim infile As IO.StreamReader = IO.File.OpenText(path)
	Dim items() As String

	Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click

		items = Split(infile.ReadLine, ",")
		Dim strfmt As String = "{0,-10}{1,-20}{2,10}"

		Dim counter As Integer = 0
		Dim temp As Integer = 3

		items = Split(infile.ReadLine, ",")
		Do While infile.Peek <> -1
			items = Split(infile.ReadLine, ",")
		Loop

		For counter = LBound(items) To UBound(items)
			lstResults.Items.Add(items(counter))
			counter = counter + 1
			lstResults.Items.Add(items(counter))
			counter = counter + 1
			lstResults.Items.Add(items(counter))


			Select Case items(counter)
				Case "A"
					lstResults.Items.Add("4")
				Case "B"
					lstResults.Items.Add("3")
				Case "C"
					lstResults.Items.Add("2")
				Case "D"
					lstResults.Items.Add("1")
				Case "F"
					lstResults.Items.Add("0")

			End Select

			counter = counter + 1
		Next




	End Sub
End Class



I'm going to do the formatting later...

I know it has to be something with how I'm reading in the data to the array from the txt file... because it is giving me an error that the array is out of bounds. I have my primer read.. etc.. is there a better method to read in than stream reader???

The way i thought it should work now is to go thru the while loop till the end of file... is there any way I can just make it stop on a certain value like noMore?

I'm not asking you guys to do my homework or anything, I just need some help and can't get to campus today. Thank you =)

Is This A Good Question/Topic? 0
  • +

Replies To: Reading CSV file into array (Visual Studio 2005)

#2 Shyne151  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-January 08

Re: Reading CSV file into array (Visual Studio 2005)

Posted 21 January 2008 - 02:08 PM

I have brainfarts sometimes... got it to work, just need to format now.

Here is the code for anyone interested

		Dim i As Integer

		items = Split(infile.ReadLine, ",")
		Do While infile.Peek <> -1
			lstResults.Items.Add(items(0))
			lstResults.Items.Add(items(1))
			lstResults.Items.Add(items(2))
			Select Case items(2)
				Case "A"
					lstResults.Items.Add("4")
				Case "B"
					lstResults.Items.Add("3")
				Case "C"
					lstResults.Items.Add("2")
				Case "D"
					lstResults.Items.Add("1")
				Case "F"
					lstResults.Items.Add("0")

			End Select
			items = Split(infile.ReadLine, ",")

		Loop


Was This Post Helpful? 0
  • +
  • -

#3 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Reading CSV file into array (Visual Studio 2005)

Posted 21 January 2008 - 02:54 PM

The first problem I think is that 'items' is simply a 1D array of whatever the last CSV string was. You are simply over-writing the previous entry many times before finally dealing with the last entry in your CSV file.

Something else to remember is that 'for' loops automatically increment the count variable on each pass, so your final 'counter = counter + 1' would actually end up adding 2 to counter by the time it got back to the first line in the 'for' loop (in this case though it doesn't matter because it takes counter higher than UBound so the loop exits anyway).

Here is some code I wrote for loading a CSV into list boxes:
Dim sr As New IO.StreamReader(FileName)
Dim tempArray()

			While Not sr.EndOfStream
			   tempArray = Split(sr.Readline, ",")

			   lbxName.Items.Add(tempArray(0))
			   lbxNum.Items.Add(tempArray(1))
			   lbxAddress.Items.Add(tempArray(2))

			End While

			sr.Close()



Edit: Whoops didnt see your reply till after I posted. That'll learn me to go to lunch, then come back and write a reply without refreshing.....

This post has been edited by scalt: 21 January 2008 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 Shyne151  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-January 08

Re: Reading CSV file into array (Visual Studio 2005)

Posted 22 January 2008 - 10:08 AM

View Postscalt, on 21 Jan, 2008 - 02:54 PM, said:

The first problem I think is that 'items' is simply a 1D array of whatever the last CSV string was. You are simply over-writing the previous entry many times before finally dealing with the last entry in your CSV file.

Something else to remember is that 'for' loops automatically increment the count variable on each pass, so your final 'counter = counter + 1' would actually end up adding 2 to counter by the time it got back to the first line in the 'for' loop (in this case though it doesn't matter because it takes counter higher than UBound so the loop exits anyway).

Here is some code I wrote for loading a CSV into list boxes:
Dim sr As New IO.StreamReader(FileName)
Dim tempArray()

			While Not sr.EndOfStream
			   tempArray = Split(sr.Readline, ",")

			   lbxName.Items.Add(tempArray(0))
			   lbxNum.Items.Add(tempArray(1))
			   lbxAddress.Items.Add(tempArray(2))

			End While

			sr.Close()



Edit: Whoops didnt see your reply till after I posted. That'll learn me to go to lunch, then come back and write a reply without refreshing.....


haha thanks anyways :) here is the final code I ended up useing:
		Dim numScore As String = "blah"
		items = Split(infile.ReadLine, ",")
		Do While infile.Peek <> -1
			If items(0) <> "noMore" Then
				Select Case items(2)
					Case "A"
						numScore = "4"
					Case "B"
						numScore = "3"
					Case "C"
						numScore = "2"
					Case "D"
						numScore = "1"
					Case "F"
						numScore = "0"

				End Select
				lstResults.Items.Add(String.Format(fmtstr, items(0), items(1), items(2), numScore))
				items = Split(infile.ReadLine, ",")
			End If

		Loop




This seems like a great site, wish I would of found it last year when I first started :(
Was This Post Helpful? 0
  • +
  • -

#5 RamachandranC  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 26-March 08

Re: Reading CSV file into array (Visual Studio 2005)

Posted 26 March 2008 - 09:39 AM

Hi,

Here is a VB.NET 2005 Class that converts a CSV File into a string array.

Public Class CSVFileReader

	Private fileLineArray() As String = Nothing

	Public Sub New(ByVal path As String)
		Try
			fileLineArray = System.IO.File.ReadAllLines(path)
		Catch ex As Exception
			MsgBox(ex.Message.ToString())
		End Try
	End Sub

	Public Function GetContentArray() As String()()

		Dim fileContentArray(fileLineArray.Length - 1)() As String

		Try
			Dim i As Integer = 0
			For i = 0 To fileLineArray.Length - 1
				Dim line As String = fileLineArray(i)
				fileContentArray(i) = line.Split(",")
			Next

		Catch ex As Exception
			MsgBox(ex.Message.ToString())
		End Try

		Return fileContentArray

	End Function

End Class



Cheers,

Ram
Architect
Was This Post Helpful? 1

Page 1 of 1