please edit this code so it does not contain breaks, while(true) and BufferedReader. Also keep getting this error " *** IOException *** phonebook.text (No such file or directory) the desired output has been given in the picture. here is the code to edit: import java.io.*; import java.util.Scanner; public class Phonebook { public static void main(String[] args) { int lookupCount = 0; int revLookupCount = 0; int phoneEntryCount = 0; Scanner inputReader = new Scanner(System.in); try { File file = new File("phonebook.txt"); BufferedReader phonebookReader; PhonebookEntry phonebookEntry[] = new PhonebookEntry[100]; phonebookReader = new BufferedReader(new FileReader(file)); String line = phonebookReader.readLine(); while (line != null) { if (phoneEntryCount >= 100) { throw new Exception( "Phonebook capacity exceeded - increase size of underlying array"); } phonebookEntry[phoneEntryCount++] = new PhonebookEntry(line); line = phonebookReader.readLine(); } phonebookReader.close(); mainLoop: while (true) { System.out.print("lookup, reverse-lookup, quit (l/r/q)? "); String option = inputReader.nextLine(); if (option.equalsIgnoreCase("l")) { lookupCount++; System.out.print("last name? "); String lName = inputReader.nextLine(); System.out.print("first name? "); String fName = inputReader.nextLine(); Name name = new Name(lName, fName); for (int i = 0; i < phoneEntryCount; i++) { PhonebookEntry aPhonebookEntry = phonebookEntry[i]; if (aPhonebookEntry.lookup(name) != null) { System.out.println( aPhonebookEntry.name + "'s phone number is " + aPhonebookEntry.phoneNumber ); continue mainLoop; } } System.out.println("--Name not found\n"); } else if (option.equalsIgnoreCase("r")) { revLookupCount++; System.out.print("Phone number (nnn-nnn-nnnn)? "); String phone = inputReader.nextLine(); PhoneNumber phoneNumber; try { phoneNumber = new PhoneNumber(phone); }catch (Exception e1){ System.out.println("*** Exception *** Phone number isn't correct."); continue; } for (int i = 0; i < phoneEntryCount; i++) { PhonebookEntry aPhonebookEntry = phonebookEntry[i]; if (aPhonebookEntry.reverseLookup(phoneNumber) != null) { System.out.println( phoneNumber + " belongs to " + aPhonebookEntry.name ); continue mainLoop; } } System.out.println("-- Phone number not found"); } else if (option.equalsIgnoreCase("q")) { System.out.println("" + lookupCount + " lookups performed."); System.out.println("" + revLookupCount + " reverse lookups performed."); break; } } } catch (FileNotFoundException e) { System.out.println("*** IOException *** phonebook.text (No such file or directory)"); } catch (Exception ex) { System.out.println("*** Exception *** " + ex); ex.printStackTrace(); } } } class Name { private String lastName; private String firstName; public Name(String lastName, String firstName) { this.lastName = lastName; this.firstName = firstName; } public String toString(){ return firstName + " " + lastName; } @Override public boolean equals(Object obj) { Name otherName = (Name) obj; return otherName.lastName.equalsIgnoreCase(lastName) && otherName.firstName.equalsIgnoreCase(firstName); } } class PhoneNumber { private int countryCode; private int areaCode; private int subscriberNumber; PhoneNumber(String phoneNumber) { String temp = phoneNumber.replace(")","-"); temp = temp.replace("(",""); String[] arr1 = temp.split("-"); countryCode = Integer.parseInt(arr1[0]); areaCode = Integer.parseInt(arr1[1]); subscriberNumber = Integer.parseInt(arr1[2]); } @Override public String toString() { return String.format("(%d)%d-%d",countryCode,areaCode,subscriberNumber); } @Override public boolean equals(Object obj) { PhoneNumber phoneNumber = (PhoneNumber) obj; return countryCode == phoneNumber.countryCode && areaCode == phoneNumber.areaCode && subscriberNumber == phoneNumber.subscriberNumber; } } class PhonebookEntry { Name name; PhoneNumber phoneNumber; PhonebookEntry(String entry){ String[] entrySplit = entry.split(" "); name = new Name(entrySplit[0],entrySplit[1]); phoneNumber = new PhoneNumber(entrySplit[2]); } String lookup(Name name1){ if(name.equals(name1)) return phoneNumber.toString(); return null; } String reverseLookup(PhoneNumber phoneNumber1){ if(phoneNumber1.equals(phoneNumber)) return name.toString(); return null; } }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 7PE
icon
Related questions
icon
Concept explainers
Question

please edit this code so it does not contain breaks, while(true) and

BufferedReader. Also keep getting this error " *** IOException *** phonebook.text (No such file or directory)

the desired output has been given in the picture.

here is the code to edit:

import java.io.*;

import java.util.Scanner;

public class Phonebook {

public static void main(String[] args) {

int lookupCount = 0;

int revLookupCount = 0;

int phoneEntryCount = 0;

Scanner inputReader = new Scanner(System.in);

try {

File file = new File("phonebook.txt");

BufferedReader phonebookReader;

PhonebookEntry phonebookEntry[] = new PhonebookEntry[100];

phonebookReader = new BufferedReader(new FileReader(file));

String line = phonebookReader.readLine();

while (line != null) {

if (phoneEntryCount >= 100) {

throw new Exception(

"Phonebook capacity exceeded - increase size of underlying array");

}

phonebookEntry[phoneEntryCount++] = new PhonebookEntry(line);

line = phonebookReader.readLine();

}

phonebookReader.close();

mainLoop:

while (true) {

System.out.print("lookup, reverse-lookup, quit (l/r/q)? ");

String option = inputReader.nextLine();

if (option.equalsIgnoreCase("l")) {

lookupCount++;

System.out.print("last name? ");

String lName = inputReader.nextLine();

System.out.print("first name? ");

String fName = inputReader.nextLine();

Name name = new Name(lName, fName);

for (int i = 0; i < phoneEntryCount; i++) {

PhonebookEntry aPhonebookEntry = phonebookEntry[i];

if (aPhonebookEntry.lookup(name) != null) {

System.out.println(

aPhonebookEntry.name + "'s phone number is " +

aPhonebookEntry.phoneNumber

);

continue mainLoop;

}

}

System.out.println("--Name not found\n");

}

else if (option.equalsIgnoreCase("r")) {

revLookupCount++;

System.out.print("Phone number (nnn-nnn-nnnn)? ");

String phone = inputReader.nextLine();

PhoneNumber phoneNumber;

try {

phoneNumber = new PhoneNumber(phone);

}catch (Exception e1){

System.out.println("*** Exception *** Phone number isn't correct.");

continue;

}

for (int i = 0; i < phoneEntryCount; i++) {

PhonebookEntry aPhonebookEntry = phonebookEntry[i];

if (aPhonebookEntry.reverseLookup(phoneNumber) != null) {

System.out.println(

phoneNumber + " belongs to " +

aPhonebookEntry.name

);

continue mainLoop;

}

}

System.out.println("-- Phone number not found");

} else if (option.equalsIgnoreCase("q")) {

System.out.println("" + lookupCount + " lookups performed.");

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

break;

}

}

} catch (FileNotFoundException e) {

System.out.println("*** IOException *** phonebook.text (No such file or directory)");

} catch (Exception ex) {

System.out.println("*** Exception *** " + ex);

ex.printStackTrace();

}

}

}

class Name {

private String lastName;

private String firstName;

public Name(String lastName, String firstName) {

this.lastName = lastName;

this.firstName = firstName;

}

public String toString(){

return firstName + " " + lastName;

}

@Override

public boolean equals(Object obj) {

Name otherName = (Name) obj;

return otherName.lastName.equalsIgnoreCase(lastName)

&& otherName.firstName.equalsIgnoreCase(firstName);

}

}

class PhoneNumber {

private int countryCode;

private int areaCode;

private int subscriberNumber;

PhoneNumber(String phoneNumber) {

String temp = phoneNumber.replace(")","-");

temp = temp.replace("(","");

String[] arr1 = temp.split("-");

countryCode = Integer.parseInt(arr1[0]);

areaCode = Integer.parseInt(arr1[1]);

subscriberNumber = Integer.parseInt(arr1[2]);

}

@Override

public String toString() {

return String.format("(%d)%d-%d",countryCode,areaCode,subscriberNumber);

}

@Override

public boolean equals(Object obj) {

PhoneNumber phoneNumber = (PhoneNumber) obj;

return countryCode == phoneNumber.countryCode

&& areaCode == phoneNumber.areaCode

&& subscriberNumber == phoneNumber.subscriberNumber;

}

}

class PhonebookEntry {

Name name;

PhoneNumber phoneNumber;

PhonebookEntry(String entry){

String[] entrySplit = entry.split(" ");

name = new Name(entrySplit[0],entrySplit[1]);

phoneNumber = new PhoneNumber(entrySplit[2]);

}

String lookup(Name name1){

if(name.equals(name1))

return phoneNumber.toString();

return null;

}

String reverseLookup(PhoneNumber phoneNumber1){

if(phoneNumber1.equals(phoneNumber))

return name.toString();

return null;

}

}

In this version, you are to reimplement the functionality of version 1, but this time using the classes you
coded in Lab 3. The basic output is identical to that of version 1, but now:
The three parallel arrays are gone – replaced with a slngle array of type PhonebookEntry.
You should be reading In the entrles uslng the read method of your PhonebookEntry class (which in turn
uses the read methods of the Name and PhoneNumber classes).
Use the equals methods of the Name and PhoneNumber classes In your lookup and reverselookup
methods.
Use the toString methods to print out Informatlon.
Make 100 the capacity of your Phonebook array
Throw an exception (of class Exception) if the capacity of the Phonebook array is exceeded.
Place a try/catch around your entire main and catch both FileNotFoundExceptions and Exceptions
(remember, the order of appearance of the exception types in the catch blocks can make a difference).
Do not use
BufferedReader
whllețtrue)
breaks
The name of your appllcatlon class should be Phonebook. Also, you should submit ALL your classes (I.e.,
Name, Strip off publlc from all your class definintlons
Sample Run #1
For example, if the file phonebooktext contains:
David (123)456-7890
Keith (234)567-8901
Jackie (345)678-9012
Augenstein Moshe (456)789-0123
(567)890-1234
Tenenbaum Aaron (678)901-2345
Gerald (789)012-3456
(890)123-4567
Langsam Yedidyah (901)234-5678
Joseph (012)345-6789
Arnow
Harrow
Jones
So
Dina
Weiss
Cox
Jim
Thurm
Transcribed Image Text:In this version, you are to reimplement the functionality of version 1, but this time using the classes you coded in Lab 3. The basic output is identical to that of version 1, but now: The three parallel arrays are gone – replaced with a slngle array of type PhonebookEntry. You should be reading In the entrles uslng the read method of your PhonebookEntry class (which in turn uses the read methods of the Name and PhoneNumber classes). Use the equals methods of the Name and PhoneNumber classes In your lookup and reverselookup methods. Use the toString methods to print out Informatlon. Make 100 the capacity of your Phonebook array Throw an exception (of class Exception) if the capacity of the Phonebook array is exceeded. Place a try/catch around your entire main and catch both FileNotFoundExceptions and Exceptions (remember, the order of appearance of the exception types in the catch blocks can make a difference). Do not use BufferedReader whllețtrue) breaks The name of your appllcatlon class should be Phonebook. Also, you should submit ALL your classes (I.e., Name, Strip off publlc from all your class definintlons Sample Run #1 For example, if the file phonebooktext contains: David (123)456-7890 Keith (234)567-8901 Jackie (345)678-9012 Augenstein Moshe (456)789-0123 (567)890-1234 Tenenbaum Aaron (678)901-2345 Gerald (789)012-3456 (890)123-4567 Langsam Yedidyah (901)234-5678 Joseph (012)345-6789 Arnow Harrow Jones So Dina Weiss Cox Jim Thurm
Here is a sample execution of the program.
User Input Is In bold. Your program should replicate the prompts and output:
lookup, reverse-lookup, quit (I/r/q)? I
last name? Arnow
first name? Davld
David Arnow's phone number is (123)456-7890
lookup, reverse-lookup, quit (I/r/g)? r
phone number (nnn-nnn-nnnn)? (456)789-0123
(456)789-0123 belongs to Moshe Augenstein
lookup, reverse-lookup, quit (I/r/g)? I
last name? Welss
first name? Jerrold
-- Name not found
lookup, reverse-lookup, quit (I/r/q)? I
last name? Welss
first name? Gerald
Gerald Weiss's phone number is (789)012-3456
lookup, reverse-lookup, quit (V/r/g)? r
phone number (nnn-nnn-nnnn)? (111)123-4567
- Phone number not found
lookup, reverse-lookup, quit (I/r/a)? q
3 lookups performed
2 reverse lookups performed
Sample Run #2
If the file phonebooktext contains:
more than 100 names
Here is a sample execution of the program.
User Input Is In bold. Your program should replicate the prompts and output:
*** Exception *** Phonebook capacity exceeded - increase size of underlying array
Transcribed Image Text:Here is a sample execution of the program. User Input Is In bold. Your program should replicate the prompts and output: lookup, reverse-lookup, quit (I/r/q)? I last name? Arnow first name? Davld David Arnow's phone number is (123)456-7890 lookup, reverse-lookup, quit (I/r/g)? r phone number (nnn-nnn-nnnn)? (456)789-0123 (456)789-0123 belongs to Moshe Augenstein lookup, reverse-lookup, quit (I/r/g)? I last name? Welss first name? Jerrold -- Name not found lookup, reverse-lookup, quit (I/r/q)? I last name? Welss first name? Gerald Gerald Weiss's phone number is (789)012-3456 lookup, reverse-lookup, quit (V/r/g)? r phone number (nnn-nnn-nnnn)? (111)123-4567 - Phone number not found lookup, reverse-lookup, quit (I/r/a)? q 3 lookups performed 2 reverse lookups performed Sample Run #2 If the file phonebooktext contains: more than 100 names Here is a sample execution of the program. User Input Is In bold. Your program should replicate the prompts and output: *** Exception *** Phonebook capacity exceeded - increase size of underlying array
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT