• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create 2d Array out of CSV File, and find #of Elements to determine Array size  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
this will be my first post and im excited about it!
Im taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off.
The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.

Im still stuck in the first part where the CSV has to be imported into the Array.
My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.

The list consists of the following wifi related values:
MAC-Adress, SSID, Timestamp, Signalstrength.
These are on the list, separated by comma.
The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.

The catch is, we are not allowed to use any of the following:
java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.

So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.

Below is my Code (Its still an active construction zone):

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dennis Von Valkenburgh,

There are some points (at the beginning) needs to be mentioned:

1. Line 1. Class name pick something more descriptive, as it is a bad habit from the very first line. Maybe: ReadCsv (even that not good as a class name, but better in your case) or something similar.
2. Line 11. Array cannot expand, so on line 11 you created array of 0 length. So you have to choose some appropriate size (probably by guessing, because you can't use any collections).
3. Lines 13 - 14. Variable names should be more descriptive also. Maybe: row [z], column [s].

Try to fix these things, then we could move on.

[edit] maybe even worth to think straight away about better structure


 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Dennis,

well, at least you know that you have four columns, so that's one half solved.

Then the number of rows. A mac address has fixed length (17 characters).
The SSID can be anything between 1 and 32 characters. Do you know anything
about the TimeStamp and SignalStrength? Taking the comma's into account,
and a newline, you can calculate kind of a minimum length of each line.

Take the file size (in your example the file in question is known, divide it by
the estimated minimum line size, and you should get an upperbound for the
number of lines.

So you can now initialize an array with the estimated number of rows and 4 columns.
Then start reading the CSV. If you still get an AOOB error, then increase
the number of rows.

If they teach you a better algorithm, let us know!

Greetz,
Piet
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thanks to you both to your explanation.

1. Line 1. Class name pick something more descriptive, as it is a bad habit from the very first line. Maybe: ReadCsv (even that not good as a class name, but better in your case) or something similar.

The Class name was given to us in the assignment and cannot be changed.

2. Line 11. Array cannot expand, so on line 11 you created array of 0 length. So you have to choose some appropriate size (probably by guessing, because you can't use any collections).

I thought I would create the array like that and then increase the size by 1 each time the reader found a new element in the file.

About your suggestions to guess the size, is there really no other way? Because our Professor specifically wanted us to think about how
we can ascertain the size of the needed array. That smells like some method to me.



3. Lines 13 - 14. Variable names should be more descriptive also. Maybe: row [z], column [s].

So instead of


I do

?


Just so you know what an entry looks like here is an example:
a0:f3:c1:a0:c2:ba, "UDSSR Network", 1420705189641, -93

In the list there are over 400 entries such as these.
Since the MAC and the ssid are in such a form should they be declared as String?
And the other two as Int?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:
2. Line 11. Array cannot expand, so on line 11 you created array of 0 length. So you have to choose some appropriate size (probably by guessing, because you can't use any collections).

I thought I would create the array like that and then increase the size by 1 each time the reader found a new element in the file.

About your suggestions to guess the size, is there really no other way? Because our Professor specifically wanted us to think about how
we can ascertain the size of the needed array. That smells like some method to me.


I wouldn't guess. Think about a way to get the exact number of rows but read the file only once.


Just so you know what an entry looks like here is an example:
a0:f3:c1:a0:c2:ba, "UDSSR Network", 1420705189641, -93

In the list there are over 400 entries such as these.
Since the MAC and the ssid are in such a form should they be declared as String?
And the other two as Int?


You only want to declare something a number if you can do math on it. So for instance, an American Zip code is not an int although it is all digits. The Timestamp should probably be a java.util.Date. Signal strength is probably an int.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Knute,
Thanks for your response.

I wouldn't guess. Think about a way to get the exact number of rows but read the file only once.

I thought of several ways. One would be counting the complete number of elements in the list and dividing them by 4, this would give me the number of rows.
The problem with this approach would be that there are some elements that have no ssid or no MAC. So it might count the wrong number.

Another way of counting the rows could be to implement a method that reads through the list until the row has no more data and then goes down.
Each time it does that there would be a counter++ keeping track of the rows. This number is then returned to the Class to create the array.

Could this work?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what I wrote was based on the text in your opening post:

My problem is that I need a mechanism that figures out the needed size for the Array, creates the array,
and only then transfers the data from the CSV.


Is it now okay to first read in the CSV? (in that case it is indeed quite
possible to determine the exact size)
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:
(...)
Another way of counting the rows could be to implement a method that reads through
the list until the row has no more data and then goes down.
Each time it does that there would be a counter++ keeping track of the rows.
This number is then returned to the Class to create the array.
Could this work?

Hmm yes, but what to do with the data that has just been read? Think about ways
to store it without using a Collection.

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Dennis Von Valkenburgh wrote:
(...)
Another way of counting the rows could be to implement a method that reads through
the list until the row has no more data and then goes down.
Each time it does that there would be a counter++ keeping track of the rows.
This number is then returned to the Class to create the array.
Could this work?

Hmm yes, but what to do with the data that has just been read? Think about ways
to store it without using a Collection.



Could a second one dimensional array be created as a help to count?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with arrays is that the length is determined at creation time.
So, even a help-array would face the problem of the determination
of the size. you only know for sure what sizes you are dealing with when
all the data has been read in.

But suppose that s1 and s2 are strings, then s1 + s2 woud be a string.
Have you been told about stringbuffers? Suppose you put all the
CSV data into, say, a string. How would you determine the constituing
parts later?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Denis, I meant
Rather than "z" and "s". Later it can help avoid mistakes.

Dennis wrote:The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.

I'm wondering about this line now. How to understand it:

1. Implement method, which determines needed array size
or
2. Implement method, which gets the size of already created array

Could you post exact requirements? It could help better understand given requirements.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Denis, I meant
Rather than "z" and "s". Later it can help avoid mistakes.
(...)

It depends on Dennis native language. If Dennis native language is German,
then z and s would be like R and C for an English speaking person. (have to use
capitals here, otherwise my post doesn't get past the censor!)
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:It depends on Dennis native language. If Dennis native language is German,
then z and s would be like R and C for an English speaking person

Good point Piet, can be a case, but still would be a better for OP to use full word for "row" and "column" in German.
I've seen few threads already, when indexes are other way round.

As in English:
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:
I wouldn't guess. Think about a way to get the exact number of rows but read the file only once.

I thought of several ways. One would be counting the complete number of elements in the list and dividing them by 4, this would give me the number of rows.
The problem with this approach would be that there are some elements that have no ssid or no MAC. So it might count the wrong number.

Another way of counting the rows could be to implement a method that reads through the list until the row has no more data and then goes down.
Each time it does that there would be a counter++ keeping track of the rows. This number is then returned to the Class to create the array.

Could this work?


I would definitely read through the whole file counting rows and store the data to move into an array later. As Piet said, the trick is how to store the data. I would use a string, and StringBuffer is a good way to create a continuously growing string. The next hurtle is how to pick a "row separator" so that you can split the rows up again correctly.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really think it is a good idea to store such data as a String?
Why not store the lines in a List<String> and you can get the List to count them very simply with the List#size() method.
Alternatively, there are classes 1 2 which count lines as they go.

Why are you using StringTokenizer which is not recommended for use in new code. Use String#split to divide the line into an array of Strings; that will give you a size automatically.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
(...)
but still would be a better for OP to use full word for "row" and "column" in German.
I've seen few threads already, when indexes are other way round.
(...)

You are right, of course.

Greetz,
Piet
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you really think it is a good idea to store such data as a String?
Why not store the lines in a List<String> and you can get the List to count them very simply with the List#size() method.
Alternatively, there are classes 1 2 which count lines as they go.

Why are you using StringTokenizer which is not recommended for use in new code. Use String#split to divide the line into an array of Strings; that will give you a size automatically.


Because the assignment states that the student can't use Collections.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you really think it is a good idea to store such data as a String?
Why not store the lines in a List<String>

Because that is not allowed. See opening post.
Campbell Ritchie wrote:
Alternatively, there are classes 1 2 which count lines as they go.

Why are you using StringTokenizer which is not recommended for use in new code. Use String#split to divide the line into an array of Strings; that will give you a size automatically.

Hmm, you're giving away a lot now. In reply to Knute I was about to write that there is
a String method that would make any counting unnecessay, and the creation of
the array almost automatically..

Well, let's here what Dennis comes up with, after a good nights sleep.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! Didn't see that bit.
Are you allowed to read the file twice? That way you can read the lines once and do nothing with them, the you know what size array to put the lines into the second time you read them.

Another way to do it is to mimic ArrayList. Create an array and count; whenever the array is full create a larger array and copy the old array into it.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Well, what I wrote was based on the text in your opening post:

My problem is that I need a mechanism that figures out the needed size for the Array, creates the array,
and only then transfers the data from the CSV.


Is it now okay to first read in the CSV? (in that case it is indeed quite
possible to determine the exact size)

I can read in the CSV, but im not sure how to go about counting the entries to determine size.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Denis, I meant
Rather than "z" and "s". Later it can help avoid mistakes.

Dennis wrote:The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.

I'm wondering about this line now. How to understand it:

1. Implement method, which determines needed array size
or
2. Implement method, which gets the size of already created array

Could you post exact requirements? It could help better understand given requirements.


The requirements are the following:

- Create the class WhatsThere, whose contstructor receives the filename as a parameter. The purpose of the Constructor is to create a Datastructure into which the data is copied.
- Come up with what objectoriented Datastructure would be best to use and use the MAC or the SSID as the key (Since these are unique).
- Come up with a way to determine the size of the needed array and then create the datastructure and fill it with data.
- When the structure has been created it should be possible to find the SSID for example by using MAC and the other way around.
- For that purpose methods have to be created which I can get into later.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Dennis,

well, that changes things quite a lot!

First: what makes you think you are not allowed to use any Collection class?
True, you are supposed to create a WhatsThere class, complete with a
constructor. But it is up to you to determine what goes into this class.

And forget about arrays. How about a class called, say, 'Connection'
with members like Mac-address, et cetera, and a constructor that
takes, for instance, a string?

And can you think of a structure that could hold all these Connection
instances? And where to put that construction?

There are quite some possibilities, in short. Think about it.
Write down your ideas and let us know. Remember, we will not do
your job for you, but we love to help.

Greetz,
Piet
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Dennis,

well, that changes things quite a lot!

First: what makes you think you are not allowed to use any Collection class?
True, you are supposed to create a WhatsThere class, complete with a
constructor. But it is up to you to determine what goes into this class.

And forget about arrays. How about a class called, say, 'Connection'
with members like Mac-address, et cetera, and a constructor that
takes, for instance, a string?

And can you think of a structure that could hold all these Connection
instances? And where to put that construction?

There are quite some possibilities, in short. Think about it.
Write down your ideas and let us know. Remember, we will not do
your job for you, but we love to help.

Greetz,
Piet


Sorry I forgot to mention in that list that we are not allowed to use any of the following:
java.util.ArrayList
java.util.Arrays
java.util.Collection
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, this teacher has a strange sense of humor...

So we're back at the start then. First things first. Have you enough information
from all responses so far, to create this 2D array from the file?
Are you really really sure you must use these things?
The suggestings ranging from creating one large string with
a special separator between all the read in lines (for instance a '\n'),
reading the file twice, et cetera, with or without doing any counting?

Last question: are things like, say, HashMaps allowed, you think?

Greetz,
Piet

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid that if Collection is prohibited a Map will be prohibited too.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the simplest solution to date would be the "read file twice" option
Paricularly given the instruction

"Come up with a way to determine the size of the needed array and then create the datastructure and fill it with data."

So

- count how many lines are in the file by opening and scanning it
- create array with 'X' items one for each line in the file
- read file again this time populating the array with data

It might not be the most efficient outcome, and it is not something I would generally do for 'real' code, but for homework/assignment purposes it should suffice.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote: . . .
I can read in the CSV, but im not sure how to go about counting the entries to determine size.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:I think the simplest solution to date would be the "read file twice" option
Paricularly given the instruction

"Come up with a way to determine the size of the needed array and then create the datastructure and fill it with data."

So

- count how many lines are in the file by opening and scanning it
- create array with 'X' items one for each line in the file
- read file again this time populating the array with data

It might not be the most efficient outcome, and it is not something I would generally do for 'real' code, but for homework/assignment purposes it should suffice.



What about read through the file once appending to s StringBuffer with rows separated by a new line character, create the array, and fill it using the StringBuffer?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, this was my idea, and with a double split you can create the arrays
in a simple loop, without doing any counting.

But: aren't we giving away too much information now? It is after all
an assignment. I think it would be wise to let Dennis come up with his ideas now,
eventually with code.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use split on the output of the StringBuilder? Use StringBuilder rather than StringBuffer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!