Question: help me modify this code, there should be no while(true) or breaks. For example, if the file phonebook.text contains: Arnow David 123-456-7890 Harrow Keith 234-567-8901

help me modify this code, there should be no while(true) or breaks.

For example, if the file phonebook.text contains:

Arnow David 123-456-7890 Harrow Keith 234-567-8901 Jones Jackie 345-678-9012 Augenstein Moshe 456-789-0123 Sokol Dina 567-890-1234 Tenenbaum Aaron 678-901-2345 Weiss Gerald 789-012-3456 Cox Jim 890-123-4567 Langsam Yedidyah 901-234-5678 Thurm Joseph 012-345-6789 

Here is a sample execution of the program. User input is in bold. Your program should exactly replicate the prompts and output:

lookup, reverse-lookup, quit (l/r/q)? l last name? Arnow first name? David David Arnow's phone number is 123-456-7890 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? 456-789-0123 456-789-0123 belongs to Augenstein, Moshe lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Jerrold -- Name not found lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Gerald Gerald Weiss's phone number is 789-012-3456 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? 111-123-4567 -- Phone number not found lookup, reverse-lookup, quit (l/r/q)? q 3 lookups performed 2 reverse lookups performed

_____________________________________________________________________________________

Modify the code to not use while(true) and breaks;

import java.io.*; import java.util.*;

public class Phonebook { public static void main(String[] args) throws Exception { final String FILENAME = "phonebook.text"; final int CAPACITY = 100;

String[] names = new String[CAPACITY], numbers = new String[CAPACITY];

int size = read(FILENAME, names, numbers, CAPACITY); for (int i = 0; i < size; i++) { } Scanner scanner = new Scanner(System.in); char choice = 'n';

int lookups = 0, reverseLookUps = 0; while (true) {

System.out.print("lookup, reverse-lookup, quit (l/r/q)? "); choice = scanner.nextLine().toLowerCase().charAt(0); switch (choice) { case 'l': System.out.print("last name? "); String lastName = scanner.nextLine(); System.out.print("first name? "); String firstName = scanner.nextLine(); String numberPhone = lookupPhone(names, numbers, size, lastName + " " + firstName); if (numberPhone.length() == 0) { System.out.println("-- Name not found"); } else { System.out.println(firstName + " " + lastName + "\'s phone number is " + numberPhone); } System.out.println(""); lookups++; break; case 'r': System.out.print("phone number (nnn-nnn-nnnn)? "); String phoneNumber = scanner.nextLine(); String name = lookupName(names, numbers, size, phoneNumber); if (name.length() == 0) { System.out.println("-- Phone number not found"); } else { String firstNameLastName[] = name.split("\\s+"); System.out.println(phoneNumber + " belongs to " + firstNameLastName[0] + ", " + firstNameLastName[1]); } System.out.println(""); reverseLookUps++; break; case 'q': break; default: System.out.println("Invalid selection!"); System.out.println("");

} if (choice == 'q') break;

} System.out.println(); System.out.println(lookups + " lookups performed"); System.out.println(reverseLookUps + " reverse lookups performed");

}

static int read(String filename, String[] names, String[] numbers, int capacity) throws IOException { Scanner scanner = new Scanner(new File(filename));

int size = 0; while (scanner.hasNext()) { if (size == capacity) { System.out.println("Phonebook capacity exceeded - increase size of underlying array"); System.exit(1); } String[] line = scanner.nextLine().split("\\s+"); names[size] = line[0] + " " + line[1]; numbers[size] = line[2]; size++; } return size; }

static String lookupPhone(String[] names, String[] numbers, int size, String name) { for (int i = 0; i < size; i++) if (names[i].equalsIgnoreCase(name)) { return numbers[i]; } return ""; }

static String lookupName(String[] names, String[] numbers, int size, String phone) { for (int i = 0; i < size; i++) if (numbers[i].equalsIgnoreCase(phone)) {

return names[i]; } return ""; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!