This is for AP Computer Science A. I am using classes PhoneBook and PhoneEntry that we already made, to make a program where you can search for a contact, add a contact, and delete a contact. Here is my code for the main program:
public class RobertSmithMod10PhoneBook2 {
public static void main(String[] args) {
//Initialize scanner
Scanner s = new Scanner(System.in);
//Initialize our phone book
PhoneBook pb = new PhoneBook();
//Main loop
int choice;
do {
//Prompt for choices
System.out.println("What would you like to do? Enter 1, 2, or 3");
System.out.println("1. Search");
System.out.println("2. Add a contact");
System.out.println("3. Delete a contact");
choice = s.nextInt();
switch (choice) {
//Search
case 1:
//Prompt the user
System.out.println("Last Name? ");
//Get the last name
String lastName = s.next();
//Prompt for first name
System.out.println("First Name? ");
String firstName = s.next();
//Boolean value so we can decide to print if no matches were found
boolean noneFound = true;
//First we will search by all...if none are returned we will cut
// to last name
boolean goToLastName = true;
PhoneEntry[] arr = pb.search(firstName, lastName);
for (PhoneEntry phoneEntry : arr) {
if (phoneEntry != null) {
goToLastName = false;
noneFound = false;
System.out.println(phoneEntry.getLastName() + ", " + phoneEntry.getFirstName());
System.out.println(phoneEntry.getPhone());
}
}
//If we have to go to last name
if (goToLastName) {
PhoneEntry[] secondArr = pb.searchLastName(lastName);
for (PhoneEntry i : secondArr) {
if (i != null) {
noneFound = false;
System.out.println(i.getLastName() + ", " + i.getFirstName());
System.out.println(i.getPhone());
}
}
}
//If no matches were found
if (noneFound) System.out.println("No matches found. ");
break;
case 2:
//Prompt the user and get the new contact info
System.out.println("Last Name? ");
lastName = s.next();
System.out.println("First Name? ");
firstName = s.next();
System.out.println("Phone Number? (Format (XXX) XXX-XXXX)");
String number = s.nextLine();
//Add this contact
pb.addContact(firstName, lastName, number);
break;
case 3:
//Prompt user and get info
//Prompt the user and get the new contact info
System.out.println("Last Name? ");
lastName = s.next();
System.out.println("First Name? ");
firstName = s.next();
//Delete the contact
pb.deleteContact(firstName, lastName);
break;
default:
System.out.println("Choice error. ");
break;
}
} while (choice != 4);
}
}
My first choice is always 2 to add a contact. After I enter a contact the program doesn't quit, but I can't enter another choice. No matter what I enter after that, it does nothing.
I am trying to fix the program where I can repeatedly search, add, or delete a contact ex. I want to add a contact, search for that contact, delete it, then quit the program.