Register FAQ Calendar Search Today's Posts Mark Forums Read

Go Back   Java Programming Forums > Java Programming > New to Java

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-09-2008, 02:04 AM
Junior Member
 
Join Date: Sep 2008
Posts: 3
nadman123 is on a distinguished road
Default How do you Assign lines of a file to an aaraylist

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...
Reply With Quote
  #2 (permalink)  
Old 09-09-2008, 01:19 PM
JavaPF's Avatar
Java Programmer
 
Join Date: May 2008
Location: Deep inside the Eclipse IDE
Posts: 172
JavaPF will become famous soon enoughJavaPF will become famous soon enough
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:

How to Read a file line by line

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.
Reply With Quote
  #3 (permalink)  
Old 09-09-2008, 10:25 PM
Junior Member
 
Join Date: Sep 2008
Posts: 3
nadman123 is on a distinguished road
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:

How to Read a file line by line

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..

Quote:
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

Java Code:
 
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...
Reply With Quote
  #4 (permalink)  
Old 15-09-2008, 06:07 PM
JavaPF's Avatar
Java Programmer
 
Join Date: May 2008
Location: Deep inside the Eclipse IDE
Posts: 172
JavaPF will become famous soon enoughJavaPF will become famous soon enough
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.

Java Code:
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);
    }
   
   }
  }
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Custom Search
100 most searched terms
Search Cloud
"bad endpoint type" com.sun.xml.internal.messaging.saaj.soapexceptionimpl com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed convert arraylist to map date_format_now eclipse shortcut keys ejb3 quartz java convert double to binary java forum java forums java jtextarea bold java jtextarea color java jtextarea font java jtextarea font size java programmer forum java programmers forum java programming forum java programming forums java read last line java read last line of file java sendkeys java.security.privilegedactionexception java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad response java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed javapf javaprogrammingforums jtextarea bold jtextarea font jtextarea font color jtextarea font size programing forums programming forums reset jcombobox saaj0008 saaj0008: bad response; bad request saaj0008: bad response; not found severe: saaj0008 severe: saaj0008: bad response; bad request severe: saaj0008: bad response; not found soap java.security.privilegedactionexception soapexceptionimpl: bad endpoint type textpad java

All times are GMT. The time now is 12:05 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.