A personal phone directory contains room for first names and phone numbers for 3
ID: 3768686 • 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
Explanation / Answer
Here is the code for you. If you have any further queires, 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.equals("quit")); i++)
{
System.out.print("Enter the name for a person: ");
name = sc.next();
if(name.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--;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.