Results 1 to 8 of 8

Thread: Storing array?

  1. #1
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default 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?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    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?

  3. #3
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    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

  4. #4
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    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

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  7. #7
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    What if the worker class is abstract? Would that make any difference? Not too familiar with abstract. Worker class has subclasses HourlyWorker and SalariedWorker.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

Similar Threads

  1. Storing an integer in an array
    By sunde887 in forum New To Java
    Replies: 1
    Last Post: 01-05-2011, 06:16 AM
  2. Need help with array and storing input
    By Bewitched1 in forum New To Java
    Replies: 8
    Last Post: 07-24-2010, 06:16 PM
  3. Storing a record set into an array
    By DJCali in forum New To Java
    Replies: 2
    Last Post: 10-25-2009, 03:47 PM
  4. Storing in an Array
    By Bascotie in forum New To Java
    Replies: 10
    Last Post: 10-15-2009, 06:12 AM
  5. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •