By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,550 Members | 908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,550 IT Pros & Developers. It's quick & easy.

New student stuck with .CSVs and Arrays

P: n/a
Hello,

I'm currently trying to read a .CSV file and get all the data into an
array so I can work with it in the program.

Here is what I currently have.

Private Sub IntializeData()

Dim AL As New ArrayList

Dim sr As IO.StreamReader

'first, let's open the file
sr = IO.File.OpenText("csvstocks.txt")

Dim Entry As String

Do
Entry = sr.ReadLine

If Not Entry Is Nothing Then

Dim B

Dim data() As String = Split(Entry, ",")

B.stock = data(0)
B.numShares = data(1)
B.datePurchased = data(2)
B.purchasePrice = data(3)
B.currentPrice = data(4)

AL.Add(B)
End If

Loop Until Entry = Nothing

End Sub

I got most of this code from another post on the board, what it does is
read the first line of the .CSV into an array, which is perfect, but
then it crashes.

How do I get all lines into the array? And then when I do get that
part, can I add stuff together by doing.

B.currentPrice(1) + B.currentPrice(2)

Thanks in advance.

Nov 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"RallyDSM" <eb********@gmail.comwrote in news:1163439539.625663.94300
@m73g2000cwd.googlegroups.com:
I'm currently trying to read a .CSV file and get all the data into an
array so I can work with it in the program.
Take a look at FileHelpers (do a google search) - it's an open source
project on SourceGear. It's a text import library.
Nov 13 '06 #2

P: n/a
RallyDSM wrote:
>
I got most of this code from another post on the board, what it does is
read the first line of the .CSV into an array, which is perfect, but
then it crashes.
What do you mean by "it crashes"? Are you getting an exception? What
is the exception?

Chris

Nov 13 '06 #3

P: n/a
RallyDSM wrote:
>
Private Sub IntializeData()

Dim AL As New ArrayList

Dim sr As IO.StreamReader

'first, let's open the file
sr = IO.File.OpenText("csvstocks.txt")

Dim Entry As String

Do
Entry = sr.ReadLine

If Not Entry Is Nothing Then

Dim B

Dim data() As String = Split(Entry, ",")

B.stock = data(0)
B.numShares = data(1)
B.datePurchased = data(2)
B.purchasePrice = data(3)
B.currentPrice = data(4)

AL.Add(B)
End If

Loop Until Entry = Nothing

End Sub
I looked again and I noticed that you are not specifying the type of
object for B. What is B? Also, do yourself a favor and put Option
Strict On at the top of your code or enable it in your project.

Nov 13 '06 #4

P: n/a
Hi... if you are a student I'm going to suggest (unlike the other response)
that you don't immediately go download some library somewhere. Anybody can
cut and paste that doesn't make one a software developer. My advice is to
spend a little more time (it's painful but necessary) to solve the problem.

And here is the advice I give to every student (customized for your
particular example), "if you need to write a program to read a CSV file
don't write a program to read a CSV file." Break it all down into parts
that work and then put the parts together.

So when you take out the Split() and AL.Add() stuff could you reliably open
the CSV file and read each line? If it can't do that then the rest doesn't
matter and all the extra code is getting in your way. When it can do that
proceed.

What is Dim B?

In answer to your second question, "No" :-) You could add numbers together
if they were numbers but you have them as text. And they wouldn't be
accessed through "B" which won't exist after you exit the Do loop. You're
putting all the data into AL remember.

Again I'd suggest you create the StreamReader (an added feature would be to
check if the file actually exists first) and simply read through the thing.
Report a count of the number of lines or output the text as you read it to
confirm that you've accomplished Step 1. Then go on to Step 2.
"RallyDSM" <eb********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Hello,

I'm currently trying to read a .CSV file and get all the data into an
array so I can work with it in the program.

Here is what I currently have.

Private Sub IntializeData()

Dim AL As New ArrayList

Dim sr As IO.StreamReader

'first, let's open the file
sr = IO.File.OpenText("csvstocks.txt")

Dim Entry As String

Do
Entry = sr.ReadLine

If Not Entry Is Nothing Then

Dim B

Dim data() As String = Split(Entry, ",")

B.stock = data(0)
B.numShares = data(1)
B.datePurchased = data(2)
B.purchasePrice = data(3)
B.currentPrice = data(4)

AL.Add(B)
End If

Loop Until Entry = Nothing

End Sub

I got most of this code from another post on the board, what it does is
read the first line of the .CSV into an array, which is perfect, but
then it crashes.

How do I get all lines into the array? And then when I do get that
part, can I add stuff together by doing.

B.currentPrice(1) + B.currentPrice(2)

Thanks in advance.

Nov 13 '06 #5

P: n/a
Hi there

Tom let me to disagree with some part of your point of view, I really
belive that is very important to use some time to research and create
your own libraries to learn about how the things work, in fact while
developing the FileHelpers I learn a lot of things and Im very happy
with it and them worth all the effort.

But in the other hand some times the users only need a fast and good
way to do things and for this case the FileHelpers are perfect.

I must to said that the CSV format has a lot of exceptions and things
that are hard to implement at least for me, here you can read a post of
Leon Bambrick about the CSV parsing and all the gotchas around it.

http://secretgeek.net/csv_trouble.asp

Cheers
Marcos
http://www.filehelpers.com

On 13 nov, 15:46, "Tom Leylan" <g...@iamtiredofspam.comwrote:
Hi... if you are a student I'm going to suggest (unlike the other response)
that you don't immediately go download some library somewhere. Anybody can
cut and paste that doesn't make one a software developer. My advice is to
spend a little more time (it's painful but necessary) to solve the problem.

And here is the advice I give to every student (customized for your
particular example), "if you need to write a program to read a CSV file
don't write a program to read a CSV file." Break it all down into parts
that work and then put the parts together.

So when you take out the Split() and AL.Add() stuff could you reliably open
the CSV file and read each line? If it can't do that then the rest doesn't
matter and all the extra code is getting in your way. When it can do that
proceed.

What is Dim B?

In answer to your second question, "No" :-) You could add numbers together
if they were numbers but you have them as text. And they wouldn't be
accessed through "B" which won't exist after you exit the Do loop. You're
putting all the data into AL remember.

Again I'd suggest you create the StreamReader (an added feature would be to
check if the file actually exists first) and simply read through the thing.
Report a count of the number of lines or output the text as you read it to
confirm that you've accomplished Step 1. Then go on to Step 2.

"RallyDSM" <ebennet...@gmail.comwrote in messagenews:11*********************@m73g2000cwd.go oglegroups.com...
Hello,
I'm currently trying to read a .CSV file and get all the data into an
array so I can work with it in the program.
Here is what I currently have.
Private Sub IntializeData()
Dim AL As New ArrayList
Dim sr As IO.StreamReader
'first, let's open the file
sr = IO.File.OpenText("csvstocks.txt")
Dim Entry As String
Do
Entry = sr.ReadLine
If Not Entry Is Nothing Then
Dim B
Dim data() As String = Split(Entry, ",")
B.stock = data(0)
B.numShares = data(1)
B.datePurchased = data(2)
B.purchasePrice = data(3)
B.currentPrice = data(4)
AL.Add(B)
End If
Loop Until Entry = Nothing
End Sub
I got most of this code from another post on the board, what it does is
read the first line of the .CSV into an array, which is perfect, but
then it crashes.
How do I get all lines into the array? And then when I do get that
part, can I add stuff together by doing.
B.currentPrice(1) + B.currentPrice(2)
Thanks in advance.- Ocultar texto de la cita -- Mostrar texto de la cita -
Nov 22 '06 #6

P: n/a
Hi Marcos:

Disagreement is fine but I believe you're missing some of the subtleties.
Notice the title contains "new student" so while fast and good might be
perfect I'll guess "I downloaded FileHelpers" might get him a "C" for effort
it probably won't yield an "A". I can pretty much guarantee he won't get a
passing grade on the final unless he has an Internet connection handy.

The "goal" of the assignment is (I'm certain) not to teach someone how to
read CSV files into arrays. If that is the goal he can send me $25 and I'll
write the code for him. The goal is to learn how to declare variables,
write DO loops, use the StreamReader, etc.

He's learning the basics so he should learn to parse a file. Perhaps then
he could write his own FileHelpers one day or if nothing else be qualified
for a job as a software developer.

Tom

"MarcosMeli" <ma********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Hi there

Tom let me to disagree with some part of your point of view, I really
belive that is very important to use some time to research and create
your own libraries to learn about how the things work, in fact while
developing the FileHelpers I learn a lot of things and Im very happy
with it and them worth all the effort.

But in the other hand some times the users only need a fast and good
way to do things and for this case the FileHelpers are perfect.

I must to said that the CSV format has a lot of exceptions and things
that are hard to implement at least for me, here you can read a post of
Leon Bambrick about the CSV parsing and all the gotchas around it.

http://secretgeek.net/csv_trouble.asp

Cheers
Marcos
http://www.filehelpers.com

On 13 nov, 15:46, "Tom Leylan" <g...@iamtiredofspam.comwrote:
Hi... if you are a student I'm going to suggest (unlike the other
response)
that you don't immediately go download some library somewhere. Anybody
can
cut and paste that doesn't make one a software developer. My advice is to
spend a little more time (it's painful but necessary) to solve the
problem.

And here is the advice I give to every student (customized for your
particular example), "if you need to write a program to read a CSV file
don't write a program to read a CSV file." Break it all down into parts
that work and then put the parts together.

So when you take out the Split() and AL.Add() stuff could you reliably
open
the CSV file and read each line? If it can't do that then the rest
doesn't
matter and all the extra code is getting in your way. When it can do that
proceed.

What is Dim B?

In answer to your second question, "No" :-) You could add numbers
together
if they were numbers but you have them as text. And they wouldn't be
accessed through "B" which won't exist after you exit the Do loop. You're
putting all the data into AL remember.

Again I'd suggest you create the StreamReader (an added feature would be
to
check if the file actually exists first) and simply read through the
thing.
Report a count of the number of lines or output the text as you read it to
confirm that you've accomplished Step 1. Then go on to Step 2.

"RallyDSM" <ebennet...@gmail.comwrote in
messagenews:11*********************@m73g2000cwd.go oglegroups.com...
Hello,
I'm currently trying to read a .CSV file and get all the data into an
array so I can work with it in the program.
Here is what I currently have.
Private Sub IntializeData()
Dim AL As New ArrayList
Dim sr As IO.StreamReader
'first, let's open the file
sr = IO.File.OpenText("csvstocks.txt")
Dim Entry As String
Do
Entry = sr.ReadLine
If Not Entry Is Nothing Then
Dim B
Dim data() As String = Split(Entry, ",")
B.stock = data(0)
B.numShares = data(1)
B.datePurchased = data(2)
B.purchasePrice = data(3)
B.currentPrice = data(4)
AL.Add(B)
End If
Loop Until Entry = Nothing
End Sub
I got most of this code from another post on the board, what it does is
read the first line of the .CSV into an array, which is perfect, but
then it crashes.
How do I get all lines into the array? And then when I do get that
part, can I add stuff together by doing.
B.currentPrice(1) + B.currentPrice(2)
Thanks in advance.- Ocultar texto de la cita -- Mostrar texto de la
cita -

Nov 22 '06 #7

P: n/a
Rally,

In addition to the others Put On Option Strict.

B is obvious a structure or a class.

As it is a class than you can declare that dirty done as

Public Class B
public stock as string
public etc as string
public sub New (givenstock as string, givenetc as whatever)
stock = givenstock
etc = given etc
end sub
End class

Have a look in the documentation how to do it nice using properties.

Now you can do in your program
Dim myB as B(stock, etc0
AL.Add(myB).

However easier with CSV files is the OleDB method which gives direct a
datatable.

http://www.vb-tips.com/dbpages.aspx?...f-212f9e0de193

I hope this helps,

Cor

"RallyDSM" <eb********@gmail.comschreef in bericht
news:11*********************@m73g2000cwd.googlegro ups.com...
Hello,

I'm currently trying to read a .CSV file and get all the data into an
array so I can work with it in the program.

Here is what I currently have.

Private Sub IntializeData()

Dim AL As New ArrayList

Dim sr As IO.StreamReader

'first, let's open the file
sr = IO.File.OpenText("csvstocks.txt")

Dim Entry As String

Do
Entry = sr.ReadLine

If Not Entry Is Nothing Then

Dim B

Dim data() As String = Split(Entry, ",")

B.stock = data(0)
B.numShares = data(1)
B.datePurchased = data(2)
B.purchasePrice = data(3)
B.currentPrice = data(4)

AL.Add(B)
End If

Loop Until Entry = Nothing

End Sub

I got most of this code from another post on the board, what it does is
read the first line of the .CSV into an array, which is perfect, but
then it crashes.

How do I get all lines into the array? And then when I do get that
part, can I add stuff together by doing.

B.currentPrice(1) + B.currentPrice(2)

Thanks in advance.

Nov 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.