Reputation: 3
I have been having trouble with my removeContact method in the AddressBook class. I can get it to work when I enter a first name, but for some reason can't get it to work for the full name. This is probably simple but i'm just getting nowhere. Thanks
AddessBook Class
public class AddressBook {
private Contact contacts[] = new Contact[100]; //Array to hold all the contacts
private int count = 0; //Number of contacts in the array(in address book)
private String fileName;
AddressBook(String fileName){
this.fileName = fileName;
}
//Add Contact
public boolean addContact(Contact contact){
if(count<100){
contacts[count] = contact;
count++;
return true;
}else{
return false;
}
}
//Remove Contact
public boolean removeContact(String fullname){
for(int i = 0; i<count; i++){
if(fullname.equals(contacts[i].getFullName())){
for(int j = i; j<count; j++){
contacts[j] = contacts[j+1];
}
count--;
}else{
return false;
}
}
return true;
}
//Search Contact
//Display Contacts
public void displayContacts(){
for(int i = 0; i<count; i++){
System.out.println(contacts[i]);
}
}
}
Contact Class
public class Contact {
private String firstName;
private String lastName;
private String phone;
//Contact constructor
Contact(String firstName, String lastName, String phone){
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
}
//First name getter
public String getFirstName(){
return firstName;
}
//Last name getter
public String getLastName(){
return lastName;
}
//Full name getter
public String getFullName(){
return firstName+" "+lastName;
}
//Phone number getter
public String getPhone(){
return phone;
}
//First name mutator
public void setFirstName(String firstName){
this.firstName = firstName;
}
//Last name mutator
public void setLastName(String lastName){
this.lastName = lastName;
}
//Phone number mutator
public void setPhone(String phone){
this.phone = phone;
}
//Method to compare first and last names for similarity
public boolean compare(Object o){
Contact contact = (Contact) o;
if(firstName == contact.firstName && lastName == contact.lastName){
return false;
}else{
return true;
}
}
public String toString(){
return firstName+" "+lastName+" "+phone;
}
}
Main Class
import java.util.*;
public class Main {
public static void main(String [] args){
AddressBook addressbook = new AddressBook("Info.txt");
Scanner s = new Scanner(System.in);
boolean quit = false;
while(!quit){
System.out.println("What would you like to do?");
System.out.println(" 1) Display all contacts");
System.out.println(" 2) Add a contact");
System.out.println(" 3) Remove a contact");
System.out.println(" 4) Search a contact");
System.out.println(" 5) Exit");
switch(s.nextInt()){
case 1:
addressbook.displayContacts();
break;
case 2:
System.out.println("First Name: ");
String fName = s.next();
System.out.println("Last Name: ");
String lName = s.next();
System.out.println("Phone Number: ");
String pNumber = s.next();
addressbook.addContact(new Contact(fName, lName, pNumber));
break;
case 3:
System.out.println("Enter in full name: ");
String fullname = s.next();
addressbook.removeContact(fullname);
break;
case 4:
System.out.println("Enter the name of the contact you are looking for: ");
case 5:
quit = true;
break;
}}}}
Upvotes: 0
Views: 504
Reputation: 146
When you make your call to the "removeContact" method, you are passing as parameter only the name, not the last name; let say you create the contact "John Doe". When you search for "John Doe" you pass "John" as the name to the method but "Doe" is the next token in the scanner, so when you try to do s.nextInt() you have a problem because "Doe" is not an int.
Also, your removeContact method is not gonna work when you have more than 1 contact. You are doing a return if the first contact of the address book is not the one you are looking for.
Edit:
Try this and let me know what happens:
case 3:
System.out.println("Enter in full name: ");
String firstName = s.next();
String lastName = s.next();
addressbook.removeContact(firstName + " " + lastName);
break;
Edit 2:
// Remove Contact
public boolean removeContact(String fullname) {
for (int i = 0; i < count; i++) {
// No need for an else here
if (fullname.equals(contacts[i].getFullName())) {
for (int j = i; j < count; j++) {
contacts[j] = contacts[j + 1];
}
count--;
return true;
}
}
return false;
}
Upvotes: 1
Reputation: 12484
Ok, I ran your code and tried to remove a name , so I got this error :
Exception in thread "main" java.util.InputMismatchException
Your remove
method is not correct. You can easily have 17 Bob's in your Address Book - "Bob Smith", "Bob Jacobs", "Bob Ahmed" , whatever...
You need to fix the removeContact method to look more like this :
public boolean removeContact(String fname, String lname){
for(int i = 0; i<count; i++){
if(contacts[i].getFirstName().equals(fname) && contacts[i].getLastName().equals(lname)){
for(int j = i; j<count; j++){
contacts[j] = contacts[j+1];
}
Also, the case 3
of Main should lok like this :
case 3:
System.out.println("Enter in first name: ");
String firstname = s.next();
System.out.println("Enter in last name: ");
String lastname = s.next();
addressbook.removeContact(firstname, lastname);
break;
Upvotes: 0
Reputation: 1176
You are asking for the full name and then comparing it only with the first name:
if(fullname.equals(contacts[i].getFirstName())){
Just compare it with the full name...
Upvotes: 2