Program in java to accept 10 names and their corresponding phone number in two 1 dim array. Then accept the name from the user display the corresponding phone number. If the name is not present in the array then display the corresponding array. Please help.
1
Expert's answer
2012-10-01T10:13:59-0400
import java.util.Scanner;
public class Driver {
public static void main(String[] args) { String[] names = new String[10]; String[] phones = new String[10]; String name=""; Scanner s = new Scanner(System.in); for (int i = 0; i < 10; i++) { System.out.println("Please enter next name: "); names[i] = s.nextLine(); System.out.println("Please enter next phone: "); phones[i] = s.nextLine(); } while (true){ System.out.println("Enter name for searching: "); name=s.nextLine(); boolean found=false; for (int i=0; i<names.length; i++){ if (names[i].equals(name)){ System.out.println(phones[i]); found=true; break; } } if (found==false){ for (int i=0; i<names.length; i++){ System.out.println(names[i]); } } }
Comments
Leave a comment