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: Adding an object to Arraylist from text files

  1. #1
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Adding an object to Arraylist from text files

    Hello guys!
    This is my "Call" class (without the setters and getters):
    import java.util.ArrayList;
     
    public class Call { 
        private String callingNumber; 
        private String dialedNumber; 
        private String datetime;
        private int duration;
     
        static ArrayList <Call> calls = new ArrayList<Call>();
     
     
        public Call (String callingNumber , String dialedNumber, String datetime, int duration)
            {
                this.callingNumber = callingNumber;  
                this.dialedNumber = dialedNumber; 
                this.datetime = datetime;  
                this.duration = duration; 
                calls.add(this);
            }

    My application needs to read a text file and then create the corresponding "Call". The "document.txt" keeps the data of a call in every line (callingNumber, dialedNumber, datetime, duration)
    (e.g. 4748832, 462346214, 01-01-2012 11:55:33, 126
    64364366, 62346547, 03-03-2012 11:55:33, 65 ) .
    I thought that i should "read" the line (in my main method) and add it to the calls ArrayList. Something like this:
    public static void main (String args[]) throws IOException {
      try {
                File file1 = new File("C:\\...\\document.txt");
                Scanner Filereader1 = new Scanner(file1);
                while (Filereader1.hasNextLine()) {
     
                    String a = Filereader1.next();
                    Call.calls.add(a)                        
                 }
           }
             catch (FileNotFoundException e) {
                System.out.println("error" + e);
             }
    }

    This is not working because "Call.calls.add()" method can only add "Call" objects which the line of the document is not.
    Any suggestions?
    Thanks.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Adding an object to Arraylist from text files

    You are trying to add String but actually the arraylist is of type Call. Reference the Java 7 API and leave the old habit of creating List.
    List<Integer> list = new ArrayList<>();

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Adding an object to Arraylist from text files

    You are defining your ArrayList to only generically accept Call objects. You are trying to add a string to this collection which is a syntax error. You need to instantiate a new Call object and add that to the array using that nice constructor you made. Try splitting the string you read by the comma ',' deliminator and passing them as parameters to the constructor.

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Adding an object to Arraylist from text files

    Thankw a lot guys. I used the split() method as ChristopherLowe proposed an it worked.

Similar Threads

  1. Adding my own object to a Container...not working.
    By Tombomb in forum Object Oriented Programming
    Replies: 1
    Last Post: January 9th, 2012, 04:18 PM
  2. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  3. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM
  4. Adding entered text from one GUI to another
    By fride360 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 24th, 2011, 01:08 PM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM