stdout is not correct In some test cases: exception in thread "main" java.util.NoSuchElementException please fix this code and add a counter. The desired output is given in the picture. Also please add spaces where it belongs
- stdout is not correct
- In some test cases: exception in thread "main" java.util.NoSuchElementException
please fix this code and add a counter. The desired output is given in the picture. Also please add spaces where it belongs
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
class Name {
public Name(String last, String first) {
this.last = last;
this.first = first;
}
public Name(String first) {
this("", first);
}
public String getFirst() {
return first;
}
public String getLast() {
return last;
}
public String getFormal() {
return first + " " + last;
}
public String getOfficial() {
return last + " " + first;
}
public String getInitials() {
return first.charAt(0) + "_" + last.charAt(0) + "_";
}
public boolean equals(Name other) {
return first.equals(other.first) && last.equals(other.last);
}
public String toString() {
return first + " " + last;
}
public static Name read(Scanner scanner) {
if (!scanner.hasNext())
return null;
String last = scanner.next();
String first = scanner.next();
return new Name(last, first);
}
private String first, last;
}
class PhoneNumber {
public PhoneNumber(String number) {
this.number = number;
}
public String getAreaCode() {
return number.substring(1, number.indexOf(")"));
}
public String getExchange() {
return number.substring(number.indexOf(")") + 1, number.indexOf("-"));
}
public String getLineNumber() {
return number.substring(number.lastIndexOf("-") + 1);
}
public boolean isTollFree() {
return number.charAt(0) == '8';
}
public boolean equals(PhoneNumber other) {
return number.equals(other.number);
}
public String toString() {
return number;
}
private String number;
public static PhoneNumber read(Scanner scanner) {
if (!scanner.hasNext())
return null;
String number = scanner.next();
return new PhoneNumber(number);
}
}
class PhonebookEntry {
private Name name;
private PhoneNumber phoneNumber;
public PhonebookEntry(Name name, PhoneNumber phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public Name getName() {
return name;
}
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
public void Display() {
if (phoneNumber.isTollFree()) {
System.out.println("Dialing (toll-free) " + name.getFormal() + ": "
+ phoneNumber.toString());
} else {
System.out.println("Dialing " + name.getFormal() + ": "
+ phoneNumber.toString());
}
}
public String toString() {
return name.toString() + ": " + phoneNumber.toString();
}
public boolean equals(PhonebookEntry other) {
return name.equals(other.name) && phoneNumber.equals(other.phoneNumber);
}
public static PhonebookEntry read(Scanner scanner) {
Name name = Name.read(scanner);
PhoneNumber phoneNumber = PhoneNumber.read(scanner);
if (name == null || phoneNumber == null)
return null;
else
return new PhonebookEntry(name, phoneNumber);
}
}
public class Phonebook {
public static void main(String[] args) {
char choice;
PhonebookEntry entries[]=new PhonebookEntry[100];
Scanner sc = new Scanner(System.in);
int count=0;
Scanner readFile=null;
try {
readFile = new Scanner(new File("phonebook.text"));
PhonebookEntry pe=PhonebookEntry.read(readFile);
while(pe!=null)
{
if(count<entries.length)
{
entries[count]=pe;
count++;
}
else
{
System.out.println("*** Exception *** Phonebook capacity exceded - increase size of underlying array");
PhonebookEntry entriesNew[]=new PhonebookEntry[entries.length*2];
for(int i=0;i<count;i++)
{
entriesNew[i]=entries[i];
}
entries=entriesNew;
entries[count]=pe;
count++;
}
pe=PhonebookEntry.read(readFile);
}
readFile.close();
while(true)
{
System.out.print("lookup, reverse-lookup, quit (l/r/q)? ");
choice=sc.next().charAt(0);
switch(choice)
{
case 'L':
case 'l':{
System.out.print("last name? ");
String lastName=sc.next();
System.out.print("first name? ");
String firstName=sc.next();
int index=searchByName(lastName,firstName,entries,count);
if(index!=-1)
{
System.out.println(entries[index].getName().getFormal()+"'s phone number is "+entries[index].getPhoneNumber().toString());
}
else
{
System.out.println("Name not found");
}
continue;
}
case 'R':
case 'r':{
System.out.print("phone number (nnn-nnn-nnnn)? ");
String number=sc.next();
int index=searchByNumber(number,entries,count);
if(index==-1)
{
System.out.println("** Phone number not found **");
}
else
{
System.out.println(number+" belongs to "+entries[index].getName().getFormal());
}
continue;
}
case 'Q':
case 'q':{
break;
}
default:{
System.out.println("** Invalid Choice **");
continue;
}
}
break;
}
} catch (FileNotFoundException e) {
System.out.println("*** IOException *** phonebook.text not found");
}
}
private static int searchByNumber(String number, PhonebookEntry[] entries,
int count) {
for(int i=0;i<count;i++)
{
if(entries[i].getPhoneNumber().toString().equalsIgnoreCase(number))
{
return i;
}
}
return -1;
}
private static int searchByName(String lastName, String firstName,
PhonebookEntry[] entries, int count) {
for(int i=0;i<count;i++)
{
if(entries[i].getName().getLast().equalsIgnoreCase(lastName) && entries[i].getName().getFirst().equalsIgnoreCase(firstName))
{
return i;
}
}
return -1;
}
}
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ffbb4ddf1-ce9d-407f-a13a-103abb2adbdc%2F179bf058-9df9-4c27-a6d4-4e4da887f17f%2Fk5x3v8_processed.png&w=3840&q=75)
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ffbb4ddf1-ce9d-407f-a13a-103abb2adbdc%2F179bf058-9df9-4c27-a6d4-4e4da887f17f%2F5vyhzv_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)