Reputation: 35
The title seems common, but I can't find answers on other related questions. Our prof tasked to complete the source code he provided us. We are working on a phonebook. This phonebook have two separate classes namely Contact
and PhoneBook
. Here is the incomplete code for the PhoneBook
.
public class PhoneBook {
Contact contact[] = new Contact[10];
int size = 10, capacity = 0;
public PhoneBook(int capacity) {
capacity++;
}
public boolean add(Contact newContact) {
if (capacity < 11) {
contact[capacity] = newContact;
return true;
} else {
return false;
}
}
public boolean delete(Contact contact) {
//fill-in code
return false;
}
public boolean update(Contact oldContact, Contact updatedContact) {
boolean found = false;
for (int i = 0; i < capacity; i++) {
// fill-in code
}
return found;
}
public Contact search(String name) {
for (int i = 0; i < capacity; i++) {
// fill-in code
// you may use the string.toLowerCase() and
// string.equals(anotherstring) methods to check
// the equality of two strings
}
return null;
}
public int getSize() {
return this.size;
}
public Contact[] getContacts() {
return this.contacts;
}
public int getCapacity() {
return this.capacity;
}
}
My problem is the method public Contact search(String name)
. We were told to check the equality of the strings in order to match the search of names in a phonebook, also to execute the delete method, updateContact and such. But, the variable name
is a String
type and the variable contact
is a Contact
type array. This is the contact class.
public class Contact {
String name;
String contactNumber;
public Contact(String name, String contactNumber) {
this.name = name;
this.contactNumber = contactNumber;
}
public String toString() {
return "Name: " + name + ", Contact No: " + contactNumber;
}
public String getName() {
return this.name;
}
public String getNumber() {
return this.contactNumber;
}
}
If I included the whole phonebook source code here it will be too long. I just need to compare name
and contact
but I can't because they are of different types. What shall I do?
Upvotes: 1
Views: 397
Reputation: 141
contact[i].name is a String type. I guess you should to try this:
public Contact search(String name){
for(int i = 0; i < capacity; i++){
if (contact[i].name.equals(name)) {
...your code...
}
Don't forget ".toLowerCase()".
Upvotes: 1
Reputation: 119
Every time you create a contact, you will have something like this to instantiate a new contact object:
Contact contact = new Contact("Alice", contactNumber);
Also, in your class, you have a getName()
method defined. If you assign a otherName
variable, as follows, it will return the value "Alice"
for you.
String otherName = contact.getName();
You can verify this by either debugging or by adding a print statement.
So to verify equality in these two objects, you can just run name.equals(otherName)
which returns true or false.
However, since this is case-sensitive, you can instead use one of the following approaches:
name.equalsIgnoreCase(otherName)
name.toLowerCase().equals(otherName.toLowerCase())
In my opinion, the first is cleaner, but based on the comments you may have to use the latter, which I find less readable.
Another approach here would be: Objects.equals(name.toLowerCase(), otherName.toLowerCase())
.
Upvotes: 2