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 5 of 5

Thread: Object creation from a input file and storing in an Array list

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Object creation from a input file and storing in an Array list

    ok, so i am a complete java noob and would be incredibly grateful if someone could show me how to create objects from a file and store them in an arraylist

    below is what i have so far
    import java.util.*;
    import java.io.*;
     
    public class People {
     
    	private String userName;
     
     
    	public People (String User)
    { 
    		userName = User;
     
    }
     
    	static ArrayList <People> Users = new ArrayList <People>();
     
    	public static void main(String[] args) 
    {
     
     
    		try 
    { 
     
    			File file1 = new File ("people.txt");
    			Scanner Filereader1 = new Scanner (file1);
    			while (Filereader1.hasNextLine())
     
     
    { 				
    				String Name = Filereader1.nextLine();
     
    				People User = new People(Name);
    				Users.add(User);
     
     
    }
     
    			System.out.println(Users);
     
    } 			
     
    		catch (FileNotFoundException e) 
    {
     
    			System.out.println("error" + e);
    }
     
     
     
     
     
     
     
    	}
     
    }
    Last edited by LabX; May 14th, 2009 at 03:26 AM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: creating objects from file input and storing in arraylist

    You should be able to just use add(obj) rather than add(index, obj).

    Other than that can you please explain your problem as I cannot really see anything wrong with it, although does your ArrayList Really want to be static?

    Chris

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating objects from file input and storing in arraylist

    what i want to do is to read input from a text file and create user objects from it

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating objects from file input and storing in arraylist

    nvm i think i got it

    error was i was not using capital letters when executing it in dos
    AAHRHHAHRAHARHRAH

    2 hours wasted
    :'-(
    Last edited by LabX; May 14th, 2009 at 03:45 AM.

  5. #5
    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: creating objects from file input and storing in arraylist

    Hello LabX and welcome to the Java Programming Forums

    Does this work as expected?

    import java.util.*;
    import java.io.*;
     
    public class People {
     
        static ArrayList<String> Users = new ArrayList<String>();
     
        public static void main(String[] args) {
     
            try {
                File file1 = new File("people.txt");
                Scanner Filereader1 = new Scanner(file1);
                while (Filereader1.hasNextLine()) {
                    int i = 0;
                    String Name = Filereader1.next();
                    Users.add(i, Name);
                    i++;
                }
     
                Object[] elements = Users.toArray();
                for (int a = 0; a < elements.length; a++) {
                    System.out.println(elements[a]);
                }
     
            }
     
            catch (FileNotFoundException e) {
                System.out.println("error" + 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. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  3. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM
  4. Program to mask input
    By sah in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2009, 06:43 PM

Tags for this Thread