In java A contact list is a place where you can store a specific contact with ot
ID: 3920350 • Letter: I
Question
In java
A contact list is a place where you can store a specific contact with other associated information such as a phone number, email address, birthday, etc. Write a program that first takes as input an integer N that represents the number of word pairs in the list to follow. Word pairs consist of a name and a phone number (both strings). That list is followed by a name, and your program should output that name's phone number. Assume that the list will always contain less than 20 word pairs.
Ex: If the input is:
the output is:
Your program must define and call the following method. The return value of GetPhoneNumber is the phone number associated with the specific contact name.
public static String GetPhoneNumber(String[] nameVec, String[] phoneNumberVec, String contactName, int arraySize)
Hint: Use two arrays: One for the string names, and the other for the string phone numbers.
Explanation / Answer
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String[] nameVec = new String[n*20];
String[] phoneNumberVec = new String[n*20];
int namePos=0, phoneNumberPos = 0;
for(int i=0; i<n; i++) {
String s[] = sc.nextLine().split(" ");
int l = s.length;
for(int j=1; j<l; j++) {
if(j%2==1) {
nameVec[namePos++] = s[j];
}
else {
phoneNumberVec[phoneNumberPos++] = s[j];
}
}
}
String target = sc.next();
System.out.println(GetPhoneNumber(nameVec, phoneNumberVec, target, namePos));
sc.close();
}
public static String GetPhoneNumber(String[] nameVec, String[] phoneNumberVec, String contactName, int arraySize) {
for(int i=0; i<arraySize; i++) {
if(nameVec[i].equals(contactName)) {
return phoneNumberVec[i];
}
}
return null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.