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 first names and phone numbers for 3

ID: 3769206 • Letter: A

Question

A personal phone directory contains room for first names and phone numbers for 30 people. Assisgn 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

//Java programming; Joyce Farrell; 8th edition, Chapter 8 p434;

//Arrays

//use a parallel array

//second questoin to be posted

Explanation / Answer

Here is the logic for you. If you need any further refinements, just get back to me....

import java.io.*;
import java.util.*;
class PhoneNumbers
{
public final static int SIZE = 30;
public static int searchName(String firstName[], String name, int count)
{
for(int i = 0; i < count; i++)
if(firstName[i].equals(name))
return i;
return -1;
}
public static void main(String[] args)
{
String[] firstName = new String[SIZE];
String[] phoneNumber = new String[SIZE];
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 10; i++)
{
System.out.print("Enter the name for person "+(i+1)+": ");   
firstName[i] = sc.next();
System.out.print("Enter the phone number of "+firstName[i]+": ");
phoneNumber[i] = sc.next();
}
String name = " ";
for(int i = 1; i < 30 && !(name.toLowerCase().equals("quit")); i++)
{
System.out.print("Enter the name for a person: ");
name = sc.next();
if(name.toLowerCase().equals("quit"))
break;
int pos = searchName(firstName, name, i);
if(pos == -1)
{
System.out.print("Enter the phone number of "+name+": ");
firstName[i] = name;
phoneNumber[i] = sc.next();
}
else
{
System.out.println("You can contact "+name+" on "+phoneNumber[pos]);
i--;
}
}
}
}

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