Java Write a program that has two parallel arrays of String objects. One of the
ID: 3600811 • Letter: J
Question
Java
Write a program that has two parallel arrays of String objects. One of the arrays should hold people's names and the other should hold their phone numbers. Here are your sample contents of both arrays:
name Array Contents phone Array Contents
"Harrison, Rose" "555-2234"
"James, Jean" "555-9098"
"Johnson, Erica" "555-3485"
"Smith, William" "555-1785"
"Smith, Brad" "555-9224"
The program needs to ask the user to enter a name or the first few characters of a name in search for the array. It should then display all of the names that match the user's input and their corresponding phone #'s. For example, if the user entered "Smith", the program should display the following names and phone #'s from the list:
Smith, William: 555-1785
Smith, Brad: 555-9224
*You must create two classes. One should hold all the methods, and the second gets the methods from the first class class and uses it into the demo/test file.
Explanation / Answer
import java.util.Scanner;
public class Myarray {
public static void main(String [] args)
{
String name[]={"Harrison, Rose","James, Jean","Smith, William","Smith, Brad" };
String phone[]={"555-2234", "555-9098", "555-1785", "555-9224"};
String n;
int i;
Scanner in=new Scanner(System.in);
System.out.print("enter a name or the first few characters of a name: ");
n=in.next();
SearchName search=new SearchName();
search.searchName(n,name,phone);
}
}
======================
public class SearchName {
public void searchName(String n,String [] name,String[] phone)
{
boolean found=false;
for(int i=0;i<name.length;i++)
{if(name[i].indexOf(n)>=0)
{System.out.println(name[i]+": "+phone[i]);
found=true;
}
}
if(!found)
System.out.println(n+" not found in the phone list");
}
}
======================
Expected output
------------------------
enter a name or the first few characters of a name: Smith
Smith, William: 555-1785
Smith, Brad: 555-9224
-----------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.