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

Thread: ArrayList weird display error

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post ArrayList weird display error

    public class PhoneBookEntry
    {
    	private String name;
    	private String phoneNumber;
     
    	/**
    	 * The PhoneBookEntry is a constructor that takes two strings as inputs
    	 * @param n for name field
    	 * @param pn for phoneNumber field
    	 */
    	public PhoneBookEntry(String n, String pn)
    	{
    		name = n;
    		phoneNumber = pn;
    	}
     
    	public void setName(String n)
    	{
    		name = n;
    	}
     
    	public void setPhone(String pn)
    	{
    		phoneNumber = pn;
    	}
     
    	public String getName()
    	{
    		return name;
    	}
     
    	public String getPhoneNumber()
    	{
    		return phoneNumber;
    	}
    }

    import java.util.Scanner;
    import java.util.ArrayList;
     
     
    public class PhoneBookDemo
    {
       public static void main(String args[])
       {
          // Constant for the numer of entries.
          final int NUM_ENTRIES = 5;
     
          // Create an ArrayList, named list, to hold PhoneBookEntry objects.
     
          ArrayList <PhoneBookEntry> list = new ArrayList<PhoneBookEntry>();
     
     
          System.out.println("I'm going to ask you to enter " +
                             NUM_ENTRIES + " names and phone numbers.");
          System.out.println();
     
          // Store PhoneBookEntry objects in the ArrayList.
          for (int i = 0; i < NUM_ENTRIES; i++)
          {
             list.add( getEntry() );
             System.out.println();
          }
     
          System.out.println("Here's the data you entered:");
     
          // Display the data stored in the ArrayList.
          for (int i = 0; i < list.size(); i++)
          {
             displayEntry(list.get(i));
          }
       }
     
       /**
          The getEntry method creates a PhoneBookEntry object
          populated with data entered by the user.
          @return A reference to the object.
       */
       public static PhoneBookEntry getEntry()
       {
            //Creates new scanner objects
            Scanner keyboard = new Scanner(System.in);
     
            //Prompts user to enter name and number
            System.out.println("Enter name: ");
            String input = keyboard.nextLine();
            System.out.println("Enter phone number associated: ");
            String input2 = keyboard.nextLine();
     
            //Apply user inputs in a new PhoneBookEntry object
            PhoneBookEntry entry = new PhoneBookEntry(input,input2);
     
            //Returns the reference object
            return entry;
       }
     
       /**
          The displayEntry method displays the data stored
          in a PhoneBookEntry object.
          @param entry The entry to display.
       */
       public static void displayEntry(PhoneBookEntry entry)
       {
            System.out.println(entry);
     
       }
    }

    - Basically the PhoneBookEntry class is the method class with field "name" and "phone#".
    - Main class is the demo, which creates PhoneBookEntry objects and stores it in an arraylist
    I am required to use ArrayList by the assignment and also use the methods in the demo class (they are required)
    I am little confusing in passing ArrayList in methods.

    I am not sure how to display entries in displayEntry() in main class.

    ERROR DISPLAYED:

    Here's the data you entered:
    PhoneBookEntry@34b23d12
    PhoneBookEntry@21c783c5
    PhoneBookEntry@319c0bd6
    PhoneBookEntry@7bcd280b
    PhoneBookEntry@5a0029ac


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: ArrayList weird display error

    Here's the data you entered:
    PhoneBookEntry@34b23d12
    PhoneBookEntry@21c783c5
    PhoneBookEntry@319c0bd6
    PhoneBookEntry@7bcd280b
    PhoneBookEntry@5a0029ac
    Those weird @-things are what you get when you say System.out.println(entry).

    That's because what println() does is call the toString() method of entry and print whatever it returns. Your PhoneBookEntry does not declare a toString() method, so the class "inherits" it from the Object class. And the toString() of the Object class prints the class name (in this case PhoneBookEntry) @ followed by a number that is supposed to be unique for each object.

    In order to get a nicer looking output you should write a toString() method for PhoneBookEntry:

    public class PhoneBookEntry
    {
        private String name;
        private String phoneNumber;
     
        // etc
     
        @Override
        public String toString() 
        {
            // your code here!
            return "some string";
        }
    }

    The contents of the method can be anything you like (that returns a string). So experiment a bit.

    I use @Override to tell the compiler that I mean to override a method from the Object parent class. Putting that in is optional, but it helps pick up any mistakes like typos in the method name.

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

    Default Re: ArrayList weird display error

    hi

    you just need to get the name and the phone number from the PhoneBookEntry object when you display.
    i.e in the println statement inside displayEntry function you need to use
    System.out.println(entry.getName() + " " + entry.getPhoneNumber());

    Hope this solves your problem

Similar Threads

  1. Weird Error with java objectoutput/input streams
    By SparX in forum Java Networking
    Replies: 5
    Last Post: March 31st, 2013, 01:53 AM
  2. Exception error display with one input and not the other
    By worldchamp in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 4th, 2012, 01:45 PM
  3. ArrayList problem, help me to find out the error!
    By jinanzhaorenbo in forum Exceptions
    Replies: 1
    Last Post: July 30th, 2012, 10:55 PM
  4. How to display error message box
    By jasonxman in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 21st, 2011, 02:47 PM
  5. How do I display 4 items at a time of an ArrayList?
    By JavaStruggler in forum Member Introductions
    Replies: 5
    Last Post: August 9th, 2011, 02:33 PM