Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A personal phone directory contains room for the first names and phone numbers f

ID: 3844112 • Letter: A

Question

A personal phone directory contains room for the first names and phone numbers for 30 people. Assign names and phone numbers for the first 10 people. Prompt the user for a name, and if the name is found in the list, display the corresponding phone number. If the name is not found in the list, prompt the user for a phone number, and add the new name and phone number to the list. Continue to prompt the user for names until the user enters "quit". After the arrays are full (containing 30 names), do not allow the user to add new entries. Save the file as PhoneNumbers.java.

Explanation / Answer

import java.io.InputStreamReader;

import java.util.Scanner;

public class PhoneNumbers {

   private static String[] names = new String[30];

   private static String[] numbers = new String[30];

   private static int count = 0;

   

   private static String search(String name){ //method to search names

      for (int i = 0; i < count; i++)

          if (name.equalsIgnoreCase(names[i]))

              return numbers[i];

      return null;

   }

   

   private static boolean add(String name, String number) { //method to add new contact information

      if (count == 30)

          return false;

      names[count] = name;

      numbers[count] = number;

      count++;

      return true;

   }

   

   private static void add10() {//to add 10 contact for 1st run

      add("Marry Jones", "7845123265");

      add("Jonny Golmes", "7414474147");

      add("Rahul Saxena", "+918902757555");

      add("Vincent Jacobs", "14774141");

      add("Martina Mark", "74756890211");

      add("Md. Aquib", "275867486");

      add("Jeyong", "2321098090");

      add("Paikhana Pacche", "1097109721");

      add("Sonu Nigam","+878412589741");

      add("Ricky Christ", "234505199");

   }

   

   public static void main(String args[]){

      if (count == 0)

          add10();

      Scanner sc = new Scanner(new InputStreamReader(System.in));

      System.out.println("Enter a name to search: ");

      String name = sc.nextLine();

      while (!name.equalsIgnoreCase("quit")) { //untill user enters quit program will loop

          String number = search(name);

          if (number == null){//if number is null, name is not present

              System.out.println("Name not found. Please enter the number for " + name + ": ");

              number = sc.nextLine();

              if (add(name,number)) //CHK if the contact is added

                  System.out.println("Contact added successfully");

              else // contact adding failed

                  System.out.println("Cannot add new contact. Phonebook full");

          }

          else

              System.out.println("Found contact: "+name+": "+number);

          System.out.println("Enter a name to search: ");

          name = sc.nextLine();

      }

   }

}

I tried my best to keep the answer as simple as possible. I have also commented the code to make things easy. Incase you face any difficulty, please feel free to comment below. I shall be glad to help you with the same.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote