Question:
Need help with toString method in java telephone directory program?
?
2012-04-16 19:50:55 UTC
I two java classes that make up a telephone directory project. The entry class has the private variables of name, address, and number with the accessor and mutator methods. The database class stores the entries in an array, but i need to convert the items in the database to a string format. How do I do that?

Classes are below

public class Database {

private Entry [] array;
private int count;

public Database(){
array = new Entry[100];
count = 0;

}

public void add(Entry item){
array[count] = item;
count++;
}

public Entry findNumber(String number){
for (int i = 0; i < count; i++)
{
if (array[i].getTelephone().equals(number))
return array[i];
}
return null;
}

public Entry findName(String name){
for (int j = 0; j < count; j++){
if (array[j].getName().equals(name))
return array[j];
}
return null;

}

public String toString(){
???????????????????????
}

}



public class Entry {
private String name;
private String address;
private String telephone;

public Entry(String name, String address, String telephone) {
this.name = name;
this.address = address;
this.telephone = telephone;
}//end constructor

public String getName(){
return name;
}

public String getAddress(){
return address;
}

public String getTelephone(){
return telephone;
}

public void setName(String name){
this.name = name;
}

public void setAddress(String address){
this.address = address;
}

public void setTelephone(String telephone){
this.telephone = telephone;
}

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


}
Three answers:
James Bond
2012-04-16 19:58:37 UTC
You have written toString in Entry class. You have an array of Entry objects in database class.

You can go on contactenate and generate the string if you want.



public String toString(){

//???????????????????????

String X="";

for(int i=0;i


return X;

}
Simon
2016-08-10 17:00:19 UTC
2
2015-01-24 13:01:17 UTC
reverse phone number search compiles hundreds of millions of phone book records to help locate the owner's name, location, time zone, email and other public information.



Use a reverse phone lookup to:

Get the identity of an unknown caller.

Identify an area code.

Recall the name of a person whose number you wrote down.

Identify an unfamiliar phone number that shows up on your bill.

https://tr.im/721a7


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...