473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

New student stuck with .CSVs and Arrays

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
7 1345
"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
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
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
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
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 I´m 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
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 I´m 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Kirk Bevins | last post by:
Hello, new to posting, got a dilema in c++. I cant seem to create new instances of my student class. The idea is to make a database where the user inputs surnames and library card numbers etc. The...
6
by: laj | last post by:
HI, Excuse this new question, I am trying to impress my wife by putting a db together for her dance studio. I put a table with all students new and old with fields including name and address and...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
11
by: xxbabysue123xx | last post by:
Heres the problem: Create a class Student with instance data name, studentNumber, class (where class is a String containing one of the following: “Freshman”, “Sophomore”, “Junior”, “Senior”. ...
2
by: JKChan | last post by:
Hi, I'm an A2 ICT student in second year of college right now and I am working on my database project as my coursework. Its about a dance school with 4 tables Student, Class, Teacher and Enrolment. ...
3
by: Synapse | last post by:
hi everyone..im trying to create a student list program using linked list that will display all my info of students..but it seems theres a little prob. after i enter my first student the program will...
1
by: Cainnech | last post by:
Hi guys, I'm a bit stuck in an infinite loop. What I'm trying to do in the code below is to compare two arrays and if there's an item in array ALGEMEEN that is not in array ITEMS, I want to add it...
13
Steel546
by: Steel546 | last post by:
Let me begin, I am a college student in a basic programming class and I honestly have a tough time learning Java. I'm here because I have a lab I'm trying to do and it's like I have a hard time...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.