0
I am getting an error at the PhoneBookMapper phoneBook - new PhoneBookMapper(file).
When I run it it say Exception in thread "main" java.lang.Error: Unresolved compilation problem: unhandled exception type Exception 

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

public class PhoneBook {
    

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); // inputs from console ra 

        File file = new File("/Users/rohitadhikari/Downloads/contacts.dat"); 

        PhoneBookMapper phoneBook = new PhoneBookMapper(file);  not a comment (this new phoneBookMapper is showing red lines at the moment)

        // creating reference ra  

        while(true) { // for loop to keep presenting menu ra 

        displayMenu(); // display menu for user ra  

        Menu menu = new Menu(input.nextLine()); // creates menu objects and takes it ra 

        System.out.println(); // skips line for better readability ra 

        if (menu.isExit()) { // If user entered third selection to exit ra 

        System.out.printf("\nExit selected, have a nice day!"); 

        break; 

        } 

        else if (menu.getChoice().equals("1")) { // user entered 1 ra 

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

        String target = input.nextLine(); // stores users target in variable ra 

        target = target.strip(); // strips leading and trailing white spaces   

        System.out.println(); // skips line for better readability ra  

        phoneBook.linearSearchAndPrintint(target); // method that searched for target and prints the target ra 

        System.out.println("\n\n\n"); // moves chart up in console  

        } 

        else if (menu.getChoice().equals("2")) { // if user entered 2  

        phoneBook.selectionsort(); // sorts all value in the array 

        phoneBook.printHeading(); // prints out the heading of report 

        phoneBook.printInfo(); // print out the contacts  

        } 

        else { // if user enters anything else besides the accepted selection 

        System.out.println("Your choice is invalid! "); 

        } 

        } 

        input.close(); // closes reading of file  

        } 

        public static void displayMenu() { // method to show user the menu 

          

          String fmt = "%1s --- %-15s\n"; 

          System.out.printf("\n\n%5sMENU\n", " "); 

          System.out.printf("%5s----\n", " "); 

          System.out.printf(fmt,"1","Search by Last Name "); 

          System.out.printf(fmt,"2","Report Last Name  in Aplhabetical Order (Ascending)"); 

          System.out.printf(fmt,"X","Exit"); 

          System.out.print("\nEnter Selection: "); 

        } 
         class Menu { 

             

                private String choice; 

                private boolean exit; 

                public Menu () { 

                this.choice="2"; 

                exit = false; 

                } 

                public String getChoice() { 

                    return this.getChoice(); 

                    } 

                public void setExit (boolean exit) { 

                this.exit =  exit; 

                } 

                public boolean isExit() { 

                return this.isExit(); 

                } 

                public void setChoice (String choice) { 

                this.choice = choice; 

                } 

                public Menu (String choice) { 

                this.choice = choice; 

                 

                 

                if (this.choice.equalsIgnoreCase("x")) { 

                this.exit = true; 

                }else { 

                this.exit = false; 

                }} 

                } 

                

                } 


        

        class PhoneBookMapper { // class to contain menu objects  

        File file; 

        String [] firstName; 

        String [] lastName; 

        String [] street; 

        String [] city; 

        String [] zip; 

        String [] state; 

        String [] abbreviatedState; 

        int counter; 

        public PhoneBookMapper() { // default construct      

        } 

        public void printHeading() { 

        System.out.printf("%-20s%-20s%-10s%-15s%-10s%-20s\n\n", "Name", "Address", "City", "Zip", "AbbreviatedState"); 

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

        System.out.println("="); 

        } 

        System.out.println(); 

        } 
        
        public void printLine(int k) { 

        String name = this.firstName[k] + " " + this.lastName[k]; 

        System.out.printf("%-20s%-20s%-10s %-15s%-10s%9s\n\n", name, this.street[k], this.zip[k], this.city[k]); 

        } 

        public void selectionsort() { 

        for (int i = 0; i < this.lastName.length - 1; i++) { 

        String currentMin = this.lastName[i]; 

        int currentMinIndex = 1; 

        String currentMinB = this.street[i]; 

        String currentMinC = this.city[i]; 

        String currentMinD = this.state[i]; 

        String currentMinE = this.zip[i]; 

        String currentMinF = this.firstName[i]; 

        String currentMinG = this.abbreviatedState[i]; 

         

        for(int j = i+1; j < this.lastName.length; j++) { 

        if (currentMin.compareTo(this.lastName[j]) > 0) { 

        currentMin = this.lastName[j]; 

        currentMin = this.lastName[j]; 

        currentMin = this.lastName[j]; 

        currentMin = this.lastName[j]; 

        currentMin = this.lastName[j]; 

        currentMin = this.lastName[j];       

        } 

        } 

        if (currentMinIndex != 1) { 

        this.lastName[currentMinIndex] = this.lastName[i]; 

        this.street[currentMinIndex] = this.street[i]; 

        this.city[currentMinIndex] = this.city[i]; 

        this.state[currentMinIndex] = this.state[i]; 

        this.zip[currentMinIndex] = this.zip[i]; 

        this.firstName[currentMinIndex] = this.firstName[i]; 

        this.abbreviatedState[currentMinIndex] = this.abbreviatedState[i]; 

        this.lastName[i] = currentMin; 

        this.street[i] = currentMin; 

        this.city[i] = currentMin; 

        this.state[i] = currentMin; 

        this.zip[i] = currentMin; 

        this.firstName[i] = currentMin; 

        this.abbreviatedState[i] = currentMin; 

        } 

        } 

        } 

        public PhoneBookMapper (File file) throws Exception { 

        this.file = file; 

        this.counter = lengthCheck(); 

        this.firstName  = new String[this.counter]; 

        this.lastName  = new String[this.counter]; 

        this.street  = new String[this.counter]; 

        this.city  = new String[this.counter]; 

        this.zip  = new String[this.counter]; 

        this.state = new String [this.counter]; 

        this.abbreviatedState  = new String[this.counter]; 

        fillArrays(); // fills arrays with values 

        } 

        public void printInfo() { 

        for(int k= 0; k < this.counter; k++) { 

         

        String name = this.firstName[k] + " " + this.lastName[k]; 

        System.out.printf("%-20s%-20s%-10s %-15s%-10s%9s\n\n", name, this.street[k], this.zip[k], this.city[k]); 

        }} 

        public void setInstances(File file) throws Exception { // 

        this.file = file; 

        this.counter = lengthCheck(); 

        this.firstName  = new String[this.counter]; 

        this.lastName  = new String[this.counter]; 

        this.street  = new String[this.counter]; 

        this.city  = new String[this.counter]; 

        this.zip  = new String[this.counter]; 

        this.state = new String [this.counter]; 

        this.abbreviatedState  = new String[this.counter]; 

        fillArrays(); 
        
            
        

        } 

        public int lengthCheck() throws Exception { 

        Scanner inputA = new Scanner(this.file); 

        int count = 0; 

        while (inputA.hasNext()) { 

        if ( count > 0) { 

        inputA.nextLine(); 

        } 

        inputA.next(); 

        inputA.next(); 

        inputA.nextLine(); 

        inputA.nextLine(); 

        inputA.nextLine(); 

        inputA.nextLine(); 

        inputA.next(); 

        count ++; 

        } 

        inputA.close(); 

        return count; 

        } 

        public void linearSearchAndPrintint(String target) { 

        int targetIndex = -1; 

        for (int i = 0; i < this.lastName.length; i++) { 

        if ( this.lastName[i].strip().equalsIgnoreCase(target)) { 

        targetIndex = 1; 

        } 

        if (targetIndex == -1) { 

        System.out.println("Not found"); 

        return; 

        }else { 

        printHeading(); 

        printLine(targetIndex); 

        } 

        } 

        } 

        public void fillArrays () throws Exception { // starts methods to fill Arrays with data 

        Scanner inputB = new Scanner(this.file); 

        for (int i = 0; i < this.counter; i++) { 

        if (i>0) { 

        inputB.nextLine(); 

        } 

        this.firstName[i] = inputB.next(); 

        this.lastName[i] = inputB.next(); 

        inputB.nextLine(); 

        this.street[i] = inputB.nextLine(); 

        this.city[i] = inputB.nextLine(); 

        this.zip[i] = inputB.nextLine(); 

         

        if (this.state[i].strip().equalsIgnoreCase("New Jersey")){ 

        this.abbreviatedState[i] = "NJ"; 

        }else if (this.state[i].strip().equalsIgnoreCase("New York")) { 

        this.abbreviatedState[i] = "Ny"; 

        } 

        else { 

        this.abbreviatedState[i] = "NA"; 

        } 

        } 

        inputB.close(); 
        }
        
            } 
        
        
         

          

         

          

         

         

I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception

I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception
I get 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type Exception


I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception
I get 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type Exception


I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception
I get 

Exception in thread "main" java.lang.Error: Unresolv

I get 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type Exception


I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception
I get 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type Exception


I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception
I get 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type Exception


I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception

ed compilation problem: Unhandled exception type Exception


I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception
I get 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type Exception



    

0 Answers0