-
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?
-
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?
-
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
-
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
-
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.
-
Some pseudo code
Code:
declare worker array
open streams
loop
read line
split line
create worker object
add object to worker array
end loop
-
What if the worker class is abstract? Would that make any difference? Not too familiar with abstract. Worker class has subclasses HourlyWorker and SalariedWorker.
-
You can't instantiate an abstract class, however, you can test and create the correct subclass and store them all in a worker array. An array of type abstract class is possible, it contains any class that is a subclass of the type. Read up on polymorphism for a bit more info.