package pdir; import javax.swing.JOptionPane; public class PhoneDirectory { public static void main (String [] args) { String name, phNo; Contact [] contact = new Contact [3]; int index = 0; //ADD NEW CONTACT IN PHONE DIRECTORY while (index < contact.length) { name = JOptionPane.showInputDialog(null, "Enter Contact Name", "Contact Name", JOptionPane.INFORMATION_MESSAGE); phNo = JOptionPane.showInputDialog(null, "Enter Contact Phone Number", "Contact Phone Number", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "New contact name is " + name +" and phone number is " + phNo); contact[index] = new Contact(name, phNo); index++; } //PRINTING PHONE DIRECTORY - on the screen for (int i=0; i < contact.length; i++) System. out.println(contact[i].name + " " + contact[i].phoneNo); //PRINTING PHONE DIRECTORY - on a dailog box String directory=""; for (int i=0; i < contact.length; i++) directory = directory + contact[i].name + " " + contact[i].phoneNo + "\n" ; JOptionPane.showMessageDialog(null, "Contact in Phone Directory \n " + directory); // SEARCHING FOR NUMBER String searchName; searchName = JOptionPane.showInputDialog(null, "Enter Contact Name to be Search", "Search Contact", JOptionPane.INFORMATION_MESSAGE); boolean found = false; for (int i=0; i < contact.length; i++) { if (searchName.equals(contact[i].name)) { JOptionPane.showMessageDialog(null, "Contact number of " + contact[i].name +" is " + contact[i].phoneNo); found = true; break; } } if (! found ) JOptionPane.showMessageDialog(null, "Contact number of " + searchName + " does not exist"); } }