Hello.. I'm new with java and have some problem with my assignment. I want to write a class named phoneBookEntry that has field for a person name and phone number. The class should have a constructor and appropriate accessor and mutator method. Then, write a main class that create five phoneBookEntry objects and store them in an arraylist. Use a loop to display the contents of each object in the arraylist.
My problem is my code can't run properly. I dont know where is my fault.
Is there anyone can check my code??? Please help me. Thank You

This is my code.

public class phoneBookEntry {
    
    /** Creates a new instance of Main */
    private String name;
    private String number;
    public phoneBookEntry() {
        name = null;
        name = null;
    }
    public phoneBookEntry (String phoneName, String phoneNumber){
        name = phoneName;
        number = phoneNumber;
    }
    public void setName (String phoneName){
        name = phoneName;
    }
    public String getName (){
        return name;
    }
    public void setNumber(String phoneNumber){
        number = phoneNumber;
    }
    public String getNumber(){
        return number;
    }       
}

I make two classes. This is for other class.

import java.util.Scanner;
import java.util.ArrayList;
public class NewClass {
    
    /** Creates a new instance of NewClass */
    public static void main (String []args){
        phoneBookEntry bookEntry1 = new phoneBookEntry ();
        ArrayList <phoneBookEntry> phoneName = new ArrayList <phoneBookEntry>(5);
        ArrayList <phoneBookEntry> phoneNumber = new ArrayList <phoneBookEntry> (5);
        Scanner input = new Scanner (System.in);
        for (int x = 0; x < phoneName.size(); x++){
            System.out.println ("Enter the identity ");
            System.out.println ("Name : ");
            bookEntry1.setName(input.nextLine());
            for (int y = 0; y < phoneNumber.size(); y++){
                System.out.println("Number : ");
                bookEntry1.setNumber(input.nextLine());
            }
        }
        for (int z = 0; z< phoneName.size(); z++){
            System.out.println("name" +bookEntry1.getName()+ "and number "+ bookEntry1.getNumber());
        }
    }
}

Your problem is that you misunderstood concept of ArrayList populated with an object type. You do not need to create array list for variable that is inside the object separately. Just create object and add it to collection.
Plus you are asked to create 5 phone book objects not ask them from user (you are complicating your own life there)

ArrayList<PhoneBookEntry> phoneBookEntries = new ArrayList<PhoneBookEntry>();
phoneBookEntry.add(new PhoneBookEntry("Peter", "0123456"));
phoneBookEntry.add(new PhoneBookEntry("Mike", "0123457"));
phoneBookEntry.add(new PhoneBookEntry("David", "0125456"));
phoneBookEntry.add(new PhoneBookEntry("Tom", "0123456"));
phoneBookEntry.add(new PhoneBookEntry("Mark", "0523456"));

Thanks for the fast reply.
However, if i want to use set() and get() method. Can i use the set() instead of add()?

phoneBookEntry bookEntry1 = new phoneBookEntry();
bookEntry1.setName("Peter");
bookEntry1.setName("623872");
System.out.println("Name :" +bookEntry1.getName()+ "number : " + bookEntry1.getNumber());

set() and get() are for your PhoneBookENtry object add() is method of ArrayList that you need to use. There is set(int index, E element) but in your case you do not need it. Is this what you been asking about, or you had something else on your mind?

So, actually i do not need the set() and get() method? But my lecture told me to use it. He gave me this
instance variable : name, number
method : setName, setNumber, getName, getNumber

Also, set(int index, E element) use just to replace the value of index that have been assigned. i can not use it to set the value to the index, just like Array, isn't it?

Your lecturer had his reasons, but for one you do need get methods so you can do for instance printing. Set method still can be used, example you create object with only one value provided. How would you set the other one?

As for the second part in regards of array list set method, no you do not use it as in array. ArrayList set method is usually used to replace existing element in the position with new one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.