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

Thread: Adding Object to ArrayList

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Adding Object to ArrayList

    import java.util.*;
    import javax.swing.*;
    public class Library 
    {
    	int id;
    	String date;
    	String description;
    	int copies;
    	String title;
    	int bookCount;
    	int magCount;
    	int multiCount;
     
    	ArrayList<Book> newBook = new ArrayList<Book>();
    	ArrayList<Magazine> newMag = new ArrayList<Magazine>();
    	ArrayList<Multimedia> newMulti = new ArrayList<Multimedia>();
    	ArrayList<Library> newLib = new ArrayList<Library>();
     
    	public void addItem(int newId, String newDate, String newDesc, int newCopies, String newTitle, String newAuthor, String newPub, int newIsbn)
    	{
    		for(int x = 0; x < newBook.size(); x++)
    		{
    			bookCount++;
     
                            // I'am having trouble here in adding book object to my arraylist.
    			newBook.get(x) = new Book();
    			newBook.get(x).setId(newId);
    			newBook.get(x).setDate(newDate);
    			newBook.get(x).setDescription(newDesc);
    			newBook.get(x).setCopies(newCopies);
    			newBook.get(x).setTitle(newTitle);
    			newBook.get(x).setAuthor(newAuthor);
    			newBook.get(x).setPublisher(newPub);
    			newBook.get(x).setIsbn(newIsbn);
    		}		
    	}
     
            // and i think this method does not work because it cannot retrieve anything, because my addItem method isn't working
    	public void retrieveItem(int id)
    	{
    		for(int x = 0; x < bookCount; x++)
    		{
    			if(id == newBook.get(x).getID())
    			{
    				JOptionPane.showMessageDialog(null, "Book Title: " + newBook.get(x).getTitle() + "\nBook Author: " + newBook.get(x).getAuthor() + 
    				"\nBook Publisher: " + newBook.get(x).getPublisher() + "\nBook ID: " + newBook.get(x).getID() + 
    				"\nBook ISBN: " + newBook.get(x).getIsbn() + "\nDate Acquired: " + newBook.get(x).getDate()
        		    + "\nNumber of Copies: " + newBook.get(x).getCopies() + "\nDescription: " + newBook.get(x).getDescription());
    			}
     
    			else
    			{
    				JOptionPane.showMessageDialog(null, "NOT FOUND");
    			}
    		}
    	}
     
    	public void setId(int temp)
    	{
    		id = temp;
    	}
     
        public int getID()
        {
        	return id;
        }
     
        public void setDate(String temp)
        {
        	date = temp;
        }
     
        public String getDate()
        {
        	return date;
        }
     
        public void setDescription(String temp)
        {
        	description = temp;
        }
     
        public String getDescription()
        {
        	return description;
        }
     
        public void setCopies(int temp)
        {
        	copies = temp;
        }
     
        public int getCopies()
        {
        	return copies;
        }
     
        public void setTitle(String temp)
        {
        	title = temp;
        }
     
        public String getTitle()
        {
        	return title;
        }
     
     
    }


    please help me out here. i added some comments in the code where im having trouble.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

    Can you explain what trouble you are having? Post the full text of any error messages.
    Make the calls to the Book class's methods before adding the Book class object to the arraylist.

    Where does the code call the arraylist's add method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Object to ArrayList

    Quote Originally Posted by Norm View Post
    Can you explain what trouble you are having? Post the full text of any error messages.
    Make the calls to the Book class's methods before adding the Book class object to the arraylist.

    Where does the code call the arraylist's add method?

    i have no errors may my logic is wrong...
    what do you mean by "Make the calls to the Book class's methods before adding the Book class object to the arraylist"?
    it is in my main class, i did not post it because i thought it would be too long...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

            newBook.get(x).setId(newId);
    	newBook.get(x).setDate(newDate);
    What are these statements doing? It looks like they are calling the Book class's methods after the Book object has been added to the arraylist.


    Can you explain what trouble you are having?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Object to ArrayList

    Quote Originally Posted by Norm View Post
            newBook.get(x).setId(newId);
    	newBook.get(x).setDate(newDate);
    What are these statements doing? It looks like they are calling the Book class's methods after the Book object has been added to the arraylist.


    Can you explain what trouble you are having?
    those statements are for storing the necessary info fro the book object in the arraylist. (i think)

    my code cant seem to add the necessary info to my arraylist that's why i cannot retrieve it by using the retrieveItem method...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

    those statements are for storing the necessary info fro the book object in the arraylist. (i think)
    They should be using a Book object to do that.
    When all the info has been stored in the Book object, Then the Book object that now contains all the info should be added to the arraylist.

    Where does the code call the arraylist's add method?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Object to ArrayList

    how should i do that?

    the add method? you mean like this? "newBook.add(new Book(args))"???

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

    You need to create a Book object, store all the info in the Book object and then add it to the arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Object to ArrayList

    newBook.addItem(Integer.parseInt(idTF.getText()), dateTF.getText(), descriptionTF.getText(), Integer.parseInt(copiesTF.getText()), 
        			titleTF.getText(), newBook.getAuthor(), newBook.getPublisher(), newBook.getIsbn());

    thats my method of adding the necessary info the book needs to be added to my arraylist. it's in my my main class. and im using my addItem() method.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

    What class is the newBook variable? Does it have an addItem() method?
    Does the code posted in post#9 compile without errors?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Object to ArrayList

    it is in the Book class. where in the Book class extends my library class. yes it compiles with no errors.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

    it is in the Book class
    Does the addItem() method add some item to some list? It looks like it should take all the data it receives as args, create an object with the data and add that object to a list.
    Is that what it does? If it is in the Book class, what class object does it create and what list does it add that object to?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Object to ArrayList

    yes it should do that. It should create a Book object and store it in an array and the retrieveItem() method be able to retrieve what is store on the arraylist.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

    it is in the Book class.
    It should create a Book object
    If the addItem() method is in the Book class,
    then why does it create a Book object?

    Did you change the code to:
    1) create a book object
    2) add info to the book object
    3)add the book object to the arraylist

    The code for step 2 should be removed and the constructor for the Book class changed to take all the info it needs when the object is created in step 1
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Object to ArrayList

    the addItem() is in the Library class, the user should add info first before adding it as a book object to the library class.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Object to ArrayList

    Should the name of the method be: addBook() if it adds a book object to the list.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Adding an array to an arraylist
    By raja211991 in forum Collections and Generics
    Replies: 1
    Last Post: January 7th, 2013, 03:46 PM
  2. Somehow it's not adding to an ArrayList of ArrayLists.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 05:09 PM
  3. [SOLVED] Adding an object to Arraylist from text files
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: January 18th, 2012, 09:27 AM
  4. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  5. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM

Tags for this Thread