Hello. I am working on a little Java project and have gotten a little stuck. May I have a little help please? First the original class and then the test class code here. Thanks!!

public class PhoneBookEntry {

    private String name;
    private String number;
    
   PhoneBookEntry(String string, String string2 ){
        
    }
    public void setName (String phoneName){
        name = phoneName;
    }
    public String getName (){
        return name;
    }
    public void setNumber(String phoneNumber){
        number = phoneNumber;
    }
    public String getNumber(){
        return number;
    }
	public static void add(PhoneBookEntry phoneBookEntry) {
		// TODO Auto-generated method stub
		
	}       
}
import java.util.Scanner;import java.util.ArrayList;
public class TestPhoneBookEntry {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		   
		
		        
		ArrayList<PhoneBookEntry> PhoneBookEntry = 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"));
		for (int index = 0; index < PhoneBookEntry.size(); index++)
	         System.out.println(PhoneBookEntry.get(index));
     
		        
		        }
		    
		}

Recommended Answers

All 6 Replies

public class PhoneBookEntry {
     
    private String name;
    private String number;
     
    PhoneBookEntry(String string, String string2 ){
    	name = string;
    	number = string2;
    }
    public void setName (String phoneName){
    name = phoneName;
    }
    public String getName (){
    return name;
    }
    public void setNumber(String phoneNumber){
    number = phoneNumber;
    }
    public String getNumber(){
    return number;
    }
    public static void add(PhoneBookEntry phoneBookEntry) {
    // TODO Auto-generated method stub
     
    }
    }

Where are u stucked... I just modified a bit code.... i think u are stuck in printing.

jep, printing seems (one) of the logical mistakes he made.
also, the fact that he forgot to replace the standard values of his instance variables with the values he provides through the constructor, but you've already changed that.

@newcuser:
just taking a wild guess here: when you're printing your elements, you get values like this one:
<packageName>PhoneBookEntry@<seemingly meaningless characters here>
this is very much to be expected.

I'll give you a few pointers, to improve your code, and to solve this problem.

step 1: remove the static 'add' method you have in your PhoneBookEntry class. you don't need it, you don't use it, it just may confuse other people who try to read your code.

step 2: give your ArrayList another name. make it clear that it contains PhoneBookEntry-objects, but it should also have a unique name, so it is clear it is not the same as the class (I think you added your 'add' method, because you thought you were using your class there. see how easy it is to get confused?)

step 3: as Majestics pointed out: set the values in your constructor. if you don't, all your elements will be equal and produce the same output. it doesn't matter if you send different values to the constructor, if you don't apply them.

step 4: this is what I think you think you're stuck on :)
well, as I said, you think you're stuck on it, actually, you're not stuck, you've just forgotten a tiny bit. when you print an object, you'll automatically call the 'toString()' method of the class (of which the object you're trying to print is an instance). anyway, you haven't specified one. if you don't override the toString method yourself, it'll automatically take the toString method of your parent class. Since you didn't specify one explicitly, this means it's going to the toString() method of the Object class, which does indeed produces this kind of output.

all you need to do, is add a toString method of your own, in the PhoneBookEntry class, and ... that's about it. here you can find a very basic example on how to implement the toString() method: toString demo

Here is what I have now. I really need to break it into two files...Class and...TestClas...and I am not complely sure how to do it. Thank you for your input!

import java.util.*;
public class PhoneBookEntry {

	private ArrayList<PhoneEntry> al;
	
	PhoneBookEntry() {
		al = new ArrayList<PhoneEntry>();
	}
	
	void add(String name, String number) {
		al.add(new PhoneEntry(name, number));
	}
	
	public String toString() {
		String str = new String("Phone list:\n-----------\n");
		for(int i = 0; i < al.size(); ++i)
			str += al.get(i).toString() + "\n";
		return str;
	}
	
	public static void main(String[] args) {
		PhoneBookEntry pb = new PhoneBookEntry();
		pb.add("Paul McCarthy\t", "2105668876");
		pb.add("Sherlock Holmes\t", "51266778345");
		pb.add("Jim Smith\t", "5126789900");
		pb.add("George Roland\t", "2105546789");
		pb.add("Shirley Jones\t", "2102235534");
		
		System.out.println(pb);
	}
	class PhoneEntry {
		private String name, number;
		
		PhoneEntry(String name, String number) {
			this.name = name;
			this.number = number;
		}
		
		public String toString() {
			return name + ": " + number;
		}
	}
}

your files were correct the first time, all you needed to change to them was in Majestics and my previous answer.

so, try to understand what is written in there. just follow the steps in there based on your original code, and you should be just fine.

Ok,
I have removed the static method and renamed the array. I am not sure what you mean by using values for the constructor and I do not understand how to do the toString() method properly...it is a new concept to me. Here is waht I have now...and thank you for your time!

public class PhoneBookEntry {
     
    private String name;
    private String number;
     
    PhoneBookEntry(String string, String string2 ){
    	name = string;
    	number = string2;
    }
    public void setName (String phoneName){
    name = phoneName;
    }
    public String getName (){
    return name;
    }
    public void setNumber(String phoneNumber){
    number = phoneNumber;
    }
    public String getNumber(){
    return number;
    public String toString() {			
    	return name + ": " + number;		
    	}

    }
}

Here is the TestClass.

import java.util.ArrayList;
public class TestPhoneBookEntry {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		   
		
		        
		ArrayList<PhoneBookEntry> PhoneBook = new ArrayList<PhoneBookEntry>();
		
		PhoneBook.add(new PhoneBookEntry("Peter", "0123456"));
		PhoneBook.add(new PhoneBookEntry("Mike", "0123457"));
		PhoneBook.add(new PhoneBookEntry("David", "0125456"));
		PhoneBook.add(new PhoneBookEntry("Tom", "0123456"));
		PhoneBook.add(new PhoneBookEntry("Mark", "0523456"));
		for (int index = 0; index < PhoneBook.size(); index++)
	         System.out.println(PhoneBook.get(index));
     
		        
		        }
		    
		}

well, except for one little mistake, this code is exactly what you need. Here's a little part of your PhoneBookEntry class where you have an error:

public String getNumber(){
    return number;
    
     public String toString() {			
    	return name + ": " + number;		
     }

}

as you can see, because of the place where you place your closing brackets, you've put your toString method within your getNumber method.

first of all: unless your return statement is conditional, you can not place any functionality after your return statement, since it will never be reached.

also, you can never get to the toString method, because it's not property let in your class. just change the above code into:

public String getNumber(){
    return number;
}

    
public String toString() {			
    return name + ": " + number;		
}

now, for the toString method, that's all up to you. the meaning of a toString method is to give a String representation for an object. so what you have is quite OK.
you could also put something like:

public String toString(){
   return "Name = " + getName() + "   Number: " + getNumber();
}

the details and what exactly to show, that's all up to you.

commented: for OP'issue good +8
commented: Thank you very much for patiently helping this new programmer! +1
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.