Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,429 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,283 people online right now. Registration is fast and FREE... Join Now!




Reading CSV file into array (Visual Studio 2005)

 
Reply to this topicStart new topic

Reading CSV file into array (Visual Studio 2005)

Shyne151
21 Jan, 2008 - 10:41 AM
Post #1

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 8

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






CODE

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 =)

User is offlineProfile CardPM
+Quote Post

Shyne151
RE: Reading CSV File Into Array (Visual Studio 2005)
21 Jan, 2008 - 01:08 PM
Post #2

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 8

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

Here is the code for anyone interested

CODE

        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

User is offlineProfile CardPM
+Quote Post

scalt
RE: Reading CSV File Into Array (Visual Studio 2005)
21 Jan, 2008 - 01:54 PM
Post #3

D.I.C Head
Group Icon

Joined: 22 Nov, 2007
Posts: 111



Thanked: 2 times
Dream Kudos: 25
My Contributions
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:
CODE

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 Jan, 2008 - 01:56 PM
User is online!Profile CardPM
+Quote Post

Shyne151
RE: Reading CSV File Into Array (Visual Studio 2005)
22 Jan, 2008 - 09:08 AM
Post #4

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 8

QUOTE(scalt @ 21 Jan, 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:
CODE

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 smile.gif here is the final code I ended up useing:
CODE

        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 sad.gif

User is offlineProfile CardPM
+Quote Post

RamachandranC
RE: Reading CSV File Into Array (Visual Studio 2005)
26 Mar, 2008 - 08:39 AM
Post #5

New D.I.C Head
*

Joined: 26 Mar, 2008
Posts: 1

Hi,

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

CODE

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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:31AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month