A personal phone directory contains room for the first names and phone numbers f
ID: 3844154 • 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.*;
import java.util.*;
public class PhoneNumbers {
public static void main(String[] args) {
String[] fName=new String[30];
long[] phNumber=new long[30];
fName[0]="Alice";
phNumber[0]=209854;
fName[1]="Alian";
phNumber[1]=3376456;
fName[2]="BOb";
phNumber[2]=276764;
fName[3]="Rai";
phNumber[3]=8115884;
fName[4]="Joe";
phNumber[4]=343114;
fName[5]="Jia";
phNumber[5]=871114;
fName[6]="Anaya";
phNumber[6]=12321;
fName[7]="Rushi";
phNumber[7]=823234;
fName[8]="Jim";
phNumber[8]=2311014;
fName[9]="Tia";
phNumber[9]=9090114;
String ch;
String name;
long ph;
//Scanner in = new Scanner(System.in);
int n=0,count=10;
do
{
Scanner in = new Scanner(System.in);
System.out.println(" Enter the first name of a person: ");
name=in.nextLine();
int flag=0;
for(int j=0;j<count;j++)
{
if(fName[j].equalsIgnoreCase(name))
{
System.out.print(" Phone number of this person is ");
System.out.println(phNumber[j]);
flag=1;
}
}
if(flag==0)
{
if(count<30)
{
Scanner sn = new Scanner(System.in);
fName[count]=name;
System.out.println(" Enter the phone number of this person: ");
ph=sn.nextLong();
phNumber[count]=ph;
count++;
}
else
System.out.println(" Phone book is full(30 entries are already made) ");
}
Scanner sc= new Scanner(System.in);
System.out.println("If you want to quit please type quit else type no ");
ch = sc.nextLine();
n=0;
if(ch.equalsIgnoreCase("quit"))
{
System.exit(0);
n=1;
}
else
n=0;
}while(n==0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.