Java Lab: Contact List

preview_player
Показать описание
----------------------------------------------------------------------------------------
Social media:
----------------------------------------------------------------------------------------

Code in comments
Рекомендации по теме
Комментарии
Автор

import java.util.Scanner;

public class ContactList {
public static String getPhoneNumber(String[] nameVec, String[] phoneNumberVec, String contactName, int arraySize) {
for (int i = 0; i < arraySize; i++) { // array size is the user elements for N.
if { // if the name at a certain index matches the contact name (which is
// the name that the user is searching for), then the if statement returns true.
return phoneNumberVec[i]; // Then this part will return the phone number at that index.
}
}
return ""; // After the for loop is over, "" is returned.
}

public static void main(String[] args) { // The program begins reading here because this is the main method.
Scanner scnr = new Scanner(System.in); // Creating a scnr object.
int N = scnr.nextInt(); // The user specifies the number of word pairs that will be typed.

String[] names = new String[N]; // creating an array for names with a specified number of elements for N
String[] phoneNumber = new String[N]; // creating an array for names with a specified number of elements for N

for (int i = 0; i < N; i++) { // The for loop will keep iterating based on the number of elements that the user put.
names[i] = scnr.next(); // Saves a string at each index for the array reference variable "names"
phoneNumber[i] = scnr.next(); // Saves a string at each index for the array reference variable "phoneNumber"
}
System.out.println(getPhoneNumber(names, phoneNumber, scnr.next(), N)); // The program then reads this part
// and then calls getPhoneNumber method. But before it does that, the user has to input the contact name since,
// there is a scnr.next() in the print statement.

scnr.close();
}
}

collegehelper