Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to assign values from a file to an arraylist of objects?

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to assign values from a file to an arraylist of objects?

    If i have a file which contains some data..and say that i want to assign the values of every other line in the file to an arraylist of objects. How do you do it please? Thanx in advance...


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How do you Assign lines of a file to an aaraylist

    Hello nadman123.

    Welcome to the Java Programming Forums!

    What type of file are you talking about? Is it a .txt file?

    To start, id read in the file line by line. You can find out exactly how to do this here:

    http://www.javaprogrammingforums.com...line-line.html

    You can then add the strLine String to an arraylist.

    Have you coded anything so far? Please post what you have here and we will help you fill in the gaps.
    If not, post an example of the data file and I will see what I can do.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do you Assign lines of a file to an aaraylist

    Quote Originally Posted by JavaPF View Post
    Hello nadman123.

    Welcome to the Java Programming Forums!

    What type of file are you talking about? Is it a .txt file?

    To start, id read in the file line by line. You can find out exactly how to do this here:

    http://www.javaprogrammingforums.com...line-line.html

    You can then add the strLine String to an arraylist.

    Have you coded anything so far? Please post what you have here and we will help you fill in the gaps.
    If not, post an example of the data file and I will see what I can do.
    Thanx for the reply..this is what I want to do....I have a file which is called "students.dat" and in this file it contains some data about students...and this file kind of looks like this..

    5
    John Smith
    19999999
    100 Dandenong Road, Caulfield East, Vic 3145
    +61-3-99112233
    Chris Wong
    90123456
    4/6 Melrose Place, Melbourne, Vic 3000
    +61-3-98765432
    Paris Hilton
    12345678
    600, Hilton Hotel, Melbourne, Vic 3000
    +61-3-64662322
    Andy Marks
    18888888
    10, Brooks Lane, Melbourne, Vic 3000
    +61-3-99031234
    John Doe
    17654321
    9123, Bourke Street, North Melbourne, Vic 3051
    +61-3-56728324
    the first line says 5 which is the amount of student records in the file...there is the name an ID, address, and phone number there is a student class as well..what i want to do is to create an arraylist of objects from the above file...hope you get this...

    In the student class there are methods called setName, getName, setAddress, getAddres...and so on..you know the accessors and mutators with default constructor and the constructor...

    I know how to create an arraylist not from a file though.

    like

    private ArrayList<Student> students;
    students = new ArrayList<Student>();
    generateStudents();
     
    private void generateStudents(){
    students.add(new Student("John Smith", "19999999","100 Dandenong Road, Caulfield East, Vic 3145","0-99112233" ));
     
     
    .............
    ............
    ...........
    so on.....
    }

    thanx in advance...

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How do you Assign lines of a file to an aaraylist

    This should get you started..

    The code below will read in a file line by line and grab the first line which tells you the number of students. You just need to add the ArrayList & methods.

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
     
    public class Class1 {
     
     public static int numberOfStudents;
     public static void main(String[] args) {
     
        try
        {
        FileInputStream in = new FileInputStream("students.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
     
        //Read first line to isolate the number of students & convert String to int.
        String strNumberOfStudents = br.readLine();
        int numberOfStudents = Integer.parseInt(strNumberOfStudents);
     
        //Print number of students first line.
        System.out.println(numberOfStudents);
     
        while((strLine = br.readLine())!= null)
        {
         // Print each line. Put arraylist add here
         System.out.println(strLine);
        }
     
        }catch(Exception e){
         System.out.println(e);
        }
     
       }
      }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 07:55 PM