Results 1 to 3 of 3
  1. #1
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Objects From File

    I have i file containing data about clients details of about 40 clients

    I want to add the data to an existing ArrayList of Client Details I have got and scanned the file and separated the data into what i want using delimiters but now i need to add each client details in the file to my arrayList which requires me to create an object for each client in order to add to the ArrayList using the .add method.

    How do i create objects for each of the clients in the file?

    or if not possible How do i do the above scenario?

    Thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Same way you create any object:
    Java Code:
    ClassName variableName = new ClassName(parameters);
    list.add(variableName);
    // or all in one line
    list.add(new ClassName(parameters));

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

    Default

    Well you need to first create a class. Then when you create the classes you can use the constructor to generate items and add them to the arraylist

    Java Code:
    class MyClass{
      String name;
      public MyClass(String name){
        this.name = name;
      }
      //getters
      //setters
    }
    
    public class Reading{
      public static void main(String[] args){  
        ArrayList<MyClass> al = new ArrayList<MyClass>();
        //read from file
        //separate from delimeters
        MyClass t = new MyClass(name);
        al.add(t);
      }
    }
    You will have to read enough to build one class, create an instance of the class, and then add it to the list.

Similar Threads

  1. error in reading objects from file
    By sakshi in forum New To Java
    Replies: 3
    Last Post: 07-27-2009, 08:17 AM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. how to deserialize multiple objects in a file
    By xcallmejudasx in forum Advanced Java
    Replies: 11
    Last Post: 12-16-2008, 05:29 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
  •