Results 1 to 14 of 14
  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default How to print the stored content when using ArrayList

    I have a main class AddressBook where I use ArrayList<Contact> addressBook = new ArrayList<>(); to add information such as name and phone numbers in my Contact class. How do I go about accessing all the information (names, and phone numbers) that is stored in my Contact class? What happens when I print it out is it only prints out the last input of name and phone number and not the entire collection of names and phone numbers. How do I print out the contents of ArrayList? You can request my source code if you think It will help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to print the stored content when using ArrayList

    How do I go about accessing all the information (names, and phone numbers) that is stored in my Contact class?
    Add some getter methods to the Contact class.

    How do I print out the contents of ArrayList?
    Loop through the items in the list, get each object from the list one at a time and call its methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to print the stored content when using ArrayList

    What object? It only has one object because I used ArrayList. This is what I have ArrayList<Contact> addressBook = new ArrayList<>();. So the object here is the addressBook?

    XML Code:
    /**
         * The print method prints out a lists of all contacts in the address book
         * as well as the number of contacts it printed. 
         */
        public static void print(ArrayList<Contact> display) {
            for (int i = 0; i < display.size(); i++) {
                System.out.println(display.get(i));
            }
            // print the number of contacts
            System.out.println("Number of contact: " + addressBook.size());
        }
    Last edited by csanch11; 10-05-2014 at 01:55 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to print the stored content when using ArrayList

    What object?
    By object I was referring to the instances of the Contract class that are stored in the ArrayList.

    // WHAT DO I PUT HERE????????
    Get the Contact objects from the arraylist and call whatever methods to get the data and print it.
    The Contact class could have getter classes to get whatever data the code wants to print, or it could have a method that would do the printing.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to print the stored content when using ArrayList

    Is that what you mean?

    XML Code:
    public static void print(ArrayList<Contact> display) {
            for (int i = 0; i < display.size(); i++) {
                System.out.println(display.get(i));
            }
            // print the number of contacts
            System.out.println("Number of contact: " + addressBook.size());
        }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to print the stored content when using ArrayList

    What happened when you compiled and executed the code?
    Did it give you what you wanted?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to print the stored content when using ArrayList

    No, this is what it gives me.

    workbook.Contact@46e5590e
    Number of contact: 1

    The number of Contact that I input is just one when I run the program so the Number of contacts: 1 is correct, but it gives me workbook.Contact@46e5590e instead of printing out all the contacts stored inside the Contact class. Yes I do loop through the ArrayList and I also have a method inside the Contact class, the printNameAndPhone(), which prints out the name as well as the phone number but how do I incorporate the printNameAndPhone() method (located in the Contact class) inside the print() method (located inside the AddressBook class)????????

    I should probably post my code here



    My main class AddressBook

    XML Code:
    package addressbook;
    
    import java.util.Scanner;
    import java.util.ArrayList;
    
    /**
     * Author: <<Conrado Sanchez>> Date: Task:
     */
    public class AddressBook {
    
        static Scanner keys = new Scanner(System.in);   // create Scanner object
        static ArrayList<Contact> addressBook = new ArrayList<>();  // create ArrayList object
    
        public static void main(String[] args) {
            String input;   // user input 
            String input_1; // will hold the user input in lowercase 
    
            do {
                // show menu and ask for user input 
                System.out.println("~Address Book Menu~");
                System.out.println("+---------------------+");
                System.out.println("ADD     Adds a contact");
                System.out.println("FIND    Finds contacts ");
                System.out.println("PRINT   Prints contacts ");
                System.out.println("QUIT    Quits");
                System.out.println("+---------------------+");
                System.out.println("\n");
                System.out.print("--> ");
                input = keys.nextLine();
    
                // space
                System.out.println();
    
                // make input lowercase 
                input_1 = input.toLowerCase();
    
                // Make user re-enter input if it's not add, find, print, or quit
                while (!input_1.matches("add|find|print|quit")) {
                    System.out.println("Invalid command. Please re-enter one of the options below.");
                    System.out.println("~Address Book Menu~");
                    System.out.println("+---------------------+");
                    System.out.println("ADD     Adds a contact");
                    System.out.println("FIND    Finds contacts ");
                    System.out.println("PRINT   Prints contacts ");
                    System.out.println("QUIT    Quits");
                    System.out.println("+---------------------+");
                    System.out.println("\n");
                    System.out.print("--> ");
                    input = keys.nextLine();
    
                    // space
                    System.out.println();
    
                    // make input lowercase 
                    input_1 = input.toLowerCase();
                }
    
                // determine and perform what type of command the user wants to use
                if (input_1.equals("add")) {
                    String name;            // hold the name 
                    String phonenumber;     // hold phone number
    
                    // ask and get name
                    System.out.print("NAME ---> ");
                    name = keys.nextLine();
                    System.out.print("PHONE --> ");
                    phonenumber = keys.nextLine();
                    // space
                    System.out.println();
                    // send information to the add method 
                    add(name, phonenumber);
                    // space
                    System.out.println();
                } else if (input_1.equals("find")) {
    
                } else if (input_1.equals("print")) {
                    print(addressBook);
                }
    
            } while (input_1.compareTo("quit") != 0);
    
        } // end of main 
    
        /**
         * The add method prompts the user for a name and a phone number. (NOT
         * FINISHED)
         */
        public static void add(String n, String p) {
            addressBook.add(new Contact(n, p));
            Contact.printNameAndPhone();
        }
    
        /**
         * The find method prompts the user for texts it will use to search the
         * address book and it will display a lists of all contacts that contain
         * that text in either the contacts name or phone number. (NOT FINISHED)
         */
        public static void find() {
    
        }
    
        /**
         * The print method prints out a lists of all contacts in the address book
         * as well as the number of contacts it printed.
         */
        public static void print(ArrayList<Contact> display) {
            for (int i = 0; i < display.size(); i++) {
                System.out.println(display.get(i));
            }
            // print the number of contacts
            System.out.println("Number of contact: " + addressBook.size());
        }
    
    } // end of WorkBook class


    My Contact class

    XML Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package workbook;
    
    /**
     *
     * @author conrados
     */
    public class Contact {
    
        private static String name;    // hold persons name 
        private static String phone;   // hold persons phone number 
    
        // constructor 
        public Contact(String name, String phone) {
            this.name = name;
            this.phone = phone;
        }
        
        public Contact() {
            
        }
    
        // toString method displays the name and phone number of the user
        public static void printNameAndPhone() {
            System.out.println("ADDED[" + name + "; " + phone + "]");
    
        }
    }
    Last edited by csanch11; 10-05-2014 at 03:12 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to print the stored content when using ArrayList

    workbook.Contact@46e5590e
    That is the String returned by the default toString() method (from the Object class) for the Contact class. If you want to see something different there, add a toString() method to the Contact class that returns the String you want to see.
    Or you could add a special method to the Contact class and call that using the instance of the Contact class returned by the ArrayList class's get() method.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to print the stored content when using ArrayList

    Okay I added the toString() method to the Contact class and I use that method instead of the printNameAndPhone() to print the name and phone number after the user adds something to the Contact class.


    **New toString() method in the Contact class

    XML Code:
    // toString method 
        public String toString() {
            String str = "ADDED[" + name + "; " + phone + "]";
            return str;
        }

    **I did some modification inside the the add() method in my AddressBook by replacing the printNameAndPhone() with toString()
    XML Code:
        /**
         * The add method prompts the user for a name and a phone number. 
         */
        public static void add(String n, String p) {
            addressBook.add(new Contact(n, p));
            // print out the name and phone number entered by the user
            Contact.toString(); 
        }

    But when I do that It says "non-static method toString() cannot be referenced from a static context".

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to print the stored content when using ArrayList

    Java Code:
      Contact.toString();
    That is the syntax for calling a static method (static context).
    non-static method toString() cannot be referenced from a static context".
    The code needs to use an instance of the Contact class to call non-static methods.

    The fields in the Contact class should NOT be static. Each instance of the class needs to have its own values.
    If the fields are static, the same field is shared by all the instances of the class.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to print the stored content when using ArrayList

    Okay thank you I used addressBook.toString() and now it prints out the name and phone number after user input. Now my only problem is printing out all the elements in the array. As you have said before "If you want to see something different there, add a toString() method to the Contact class that returns the String you want to see." So here you are saying to loop through the ArrayList, it will look like, for (int i = 0; i < display.size(); i++) in my print() method in the AddressBook and add the toString() method that I recently created from my Contact class to the print() method to print out all the elements in the ArrayList? Below is the new print() method and what happens when I run the program. Is this what you meant?



    XML Code:
    public static void print(ArrayList<Contact> display) {
            for (int i = 0; i < display.size(); i++) {
                System.out.println(addressBook.toString());
            }
            // print the number of contacts
            System.out.println("Number of contact: " + addressBook.size());
        }


    This is what the input and output looks when I run the program. When I enter print, it gave me 4 sets of ADDED[crissy ; 989] and no [ADDED[conrado ; 808]].

    run:
    ~Address Book Menu~
    +---------------------+
    ADD Adds a contact
    FIND Finds contacts
    PRINT Prints contacts
    QUIT Quits
    +---------------------+


    --> add

    NAME ---> conrado
    PHONE --> 808

    [ADDED[conrado ; 808]]

    ~Address Book Menu~
    +---------------------+
    ADD Adds a contact
    FIND Finds contacts
    PRINT Prints contacts
    QUIT Quits
    +---------------------+


    --> add

    NAME ---> crissy
    PHONE --> 989

    [ADDED[crissy ; 989], ADDED[crissy ; 989]]

    ~Address Book Menu~
    +---------------------+
    ADD Adds a contact
    FIND Finds contacts
    PRINT Prints contacts
    QUIT Quits
    +---------------------+


    --> print

    [ADDED[crissy ; 989], ADDED[crissy ; 989]]
    [ADDED[crissy ; 989], ADDED[crissy ; 989]]
    Number of contact: 2

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to print the stored content when using ArrayList

    it gave me 4 sets of ADDED[crissy ; 989] and no [ADDED[conrado ; 808]].
    Are the variables still static? See post#10
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to print the stored content when using ArrayList

    Okay it works!!!!! I used this inside the print() method and it prints out all the contacts. I still need the find method where the application prompts the user for the text it will use to search the address book. After the user enters the text, the application displays a list of all contacts that contain that text in either the contact's name or phone number. So lets say the user enters in 808, the application searches the ArrayList for all the contacts that contains 808 in them. How would I go about accessing specific elements in the ArrayList? This would require to loop through the ArrayList again and compare the text input of the user with the elements of ArrayList.

    XML Code:
    String contactsStr = "";
            for (Contact current : display) {
                System.out.println(current);
            }

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to print the stored content when using ArrayList

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 10
    Last Post: 12-16-2011, 04:30 PM
  2. print the full content of JScrollpane
    By Mion in forum AWT / Swing
    Replies: 13
    Last Post: 11-25-2011, 04:22 PM
  3. String[] stored in ArrayList
    By JonJacobs in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 06:27 PM
  4. Class Instances stored in an ArrayList
    By Ersk in forum New To Java
    Replies: 4
    Last Post: 12-12-2009, 04:13 PM
  5. How to print the content of List
    By cassysumandak in forum New To Java
    Replies: 4
    Last Post: 10-01-2009, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •