Results 1 to 11 of 11
  1. #1
    chopo1980 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Question store data into an array from a file

    i got a class that reads from a file. ( name and age ). its working fine. it uses Tokenizer

    now i got to store this data in a diferent class called Staff

    not sure how can add the data (name and age) into new array.

    any ideas



    Thank you for time and interest.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    So, from the question, I presume you have a Staff class, which has a constructor which takes a name and an age. From this you want to store the values into an array, correct?

    Do you have code that has your current attempt? Also, where exactly is the problem? Are you having trouble creating an array of Staff objects?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes if you can show your code here, then we can comment on that.

    You had to have an object reference to the Staff class, in order to pass data to it. I hope you know about those objects, constructors and stuff.

  4. #4
    chopo1980 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    yes i have two classes reader ( reads 5 names and ages from a file),

    and staff.


    the problem is i am not sure how initialise and store the 5 values into an array.

    do i need another class staffList maybe?




    Java Code:
    	
    	public Staff(String namefile, int agefile)
    	{
    		name = namefile;
    		age = agefile;
    	}
    
    	
    
    
    	
    public String getName() {  return name; }
    
        
    public int getAge()  { return age; }
    Last edited by Eranga; 11-09-2009 at 04:42 PM. Reason: added code tags

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You can use string.split to tokenize the data. The method already returns an array.
    Then you want to create the Staff objects and add them to a List.

  6. #6
    chopo1980 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    ok i have done string.split to tokenize the data, but i am not sure how to initialise array and add the data.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You initialize an ArrayList before the loop that reads the data.
    Then inside the loop that reads the data, you create a Staff object using the values in the array that was returned by the String.split method. You then add that Staff object to the arraylist you had initialized before.

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Just to clarify, are you trying to create an array, or a list?

    Array:
    Java Code:
    int numOfStaff = 5;
    Staff[] myStaff = new Staff[numOfStaff];

    List:
    Java Code:
    List<Staff> myStaff = new ArrayList<Staff>();
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  9. #9
    chopo1980 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    it is an array.

  10. #10
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Can you post code for your latest attempt? It's much easier to give feedback on code, then to give generic advice, especially when we don't know what you have tried so far.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  11. #11
    chopo1980 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Need a solution to read and store data from a file
    By sheetalnri in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 06:43 AM
  2. Comparing two data types in order to store in array
    By gwithey in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 10:27 AM
  3. Replies: 4
    Last Post: 04-10-2009, 05:35 AM
  4. Store textfile data in an array
    By mokonji in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 05:28 PM
  5. Replies: 3
    Last Post: 04-07-2008, 06:02 PM

Posting Permissions

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