Results 1 to 8 of 8
Thread: Storing array?
- 04-13-2011, 08:55 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Storing array?
In my Hw5 class
i have a line of text from a file i need to read:
"Hourly,Bob,40,10,Wilson"
type of worker, workers name, hours worked, rate, and boss, this repeats multiple times with different info for every 'worker'.
When reading from file, i'm suppose to use the data to construct the appropriate object
(HourlyWorker, SalariedWorker, TemporaryWorker)
and store the objects into an array of Workers
How do i go about storing one line of text into an array?
and then once i get each array per line, how do you store multiple array's inside one array?
//info on worker class
constructor:
+ Worker( name : String, hoursWorked : int, hourlyRate :
double, boss : String)
and has the set and get methods of name hoursworked hourlyrate and boss
how do i go about doing this?
- 04-13-2011, 10:05 PM #2
I think your steps are going to be
-Read the file in line by line
-After you read in a line, create a String[] to hold the data by doing:
String[] data = lineReadIn.split(",");
This will give you an array that looks like this:
data[0] = "Hourly"
data[1] = "Bob"
data[2] = "40"
data[3] = "10"
data[4] = "Wilson"
The thing I can't figure out at the moment is, I know that if you want to store it in a 2d array, its going to be something like:
String[][] myTwoDArray = new String[numberOfRowsInFile][5];
I know that when you declare the 2d array, you have to declare the size, i.e the number of rows of data you have in your text file. Problem is, after you create the array for each row, you can't add it to the 2d array until you declare it (which means declaring its size), but how do you know the size of the 2d array until you go through all the rows of data? So would you have to loop through the data twice? Once to determine how big to make your array, and a 2nd time to actually add the arrays created to the 2d array? Anybody?
- 04-13-2011, 10:47 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Well i know there's an ArrayList class or something that stores multiple arrays...i just don't know how to use it but thank you for the first part
- 04-13-2011, 10:53 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
How should i go about reading the file in line by line?
Scanner?
BufferedReader?
idk how to go about doing that where i can split it line by line and then loop it to create a seperate array for each line
- 04-14-2011, 12:24 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You shouldn't be using a 2 d array. This is a messy approach. Instead you can simply use objects of type worker and store them in an array of workers.
As for reading, chain a buffered reader and a file stream together and use nextLine to extract the lines of the file.
Sehudson showed how to use split, use it and then use the correct array elements to create a worker object. Finally, add a worker object to the worker array.
- 04-14-2011, 12:26 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Some pseudo code
Java Code:declare worker array open streams loop read line split line create worker object add object to worker array end loop
- 04-14-2011, 05:25 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
What if the worker class is abstract? Would that make any difference? Not too familiar with abstract. Worker class has subclasses HourlyWorker and SalariedWorker.
- 04-14-2011, 06:10 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Storing an integer in an array
By sunde887 in forum New To JavaReplies: 1Last Post: 01-05-2011, 05:16 AM -
Need help with array and storing input
By Bewitched1 in forum New To JavaReplies: 8Last Post: 07-24-2010, 05:16 PM -
Storing a record set into an array
By DJCali in forum New To JavaReplies: 2Last Post: 10-25-2009, 02:47 PM -
Storing in an Array
By Bascotie in forum New To JavaReplies: 10Last Post: 10-15-2009, 05:12 AM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks