Question

Hello Can you help to fix the program. When running, it still show Exception in thread...

Hello Can you help to fix the program. When running, it still show

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class Contact
location: class ContactMap
   at ContactMap.main(ContactMap.java:40)
C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

************************

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

import java.util.TreeMap;

public class ContactMap

{

public static void main(String args[]) throws IOException

{

Scanner input=new Scanner(System.in);

//Create a TreeMap , to store contact object with last name as the key

Map<String, Contact> contactsMap = new TreeMap<String, Contact>();

//Prompt the user to enter the contacts file name

System.out.print("Enter the contacts file name: ");

//Read the file name

String contactsFile=input.nextLine();

//Open the file

BufferedReader in = new BufferedReader(new FileReader(contactsFile));

String line = "";

//Read all contacts into tree map

while ((line = in.readLine()) != null)

{

String columns[] = line.split("\t");

contactsMap.put(columns[1].trim(), new Contact(columns[0].trim(),columns[1].trim(),columns[2].trim(),columns[3].trim()));

}

//Run the menu

while(true)

{

int choice;

//Print the menu

System.out.println("1. Add a contact to the list");

System.out.println("2. Delete a contact from the list");

System.out.println("3. Display the entire contact list.");

System.out.println("4.Exit");

System.out.print("Enter your choice: ");

//Read the choice

choice=input.nextInt();

switch(choice)

{

case 1:

//Read new contact information

System.out.print("Enter the first name: ");

input.nextLine();

String fName=input.nextLine();

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

String lName=input.nextLine();

System.out.print("Enter the phone number: ");

String phoneNumber=input.nextLine();

System.out.print("Enter the email-id: ");

String eId=input.nextLine();

//Call the addToList method

addToList(contactsMap,new Contact(fName,lName,phoneNumber,eId));

break;

case 2:

//Prompt the user to enter the last name

System.out.print("Enter the last name to remove from the contacts: ");

input.nextLine();

//Read the last name

String phNo= input.nextLine();

//Call the deleteFromList method

deleteFromList(contactsMap,phNo);

break;

case 3:

//Call displayContacts to print all contacts

displyContacts(contactsMap);

input.nextLine();

break;

case 4:

//Exit from the program

System.exit(0);

default: System.out.println("Enter a valid choice");

}

}

}

//Adds the new Contact to the treemap.

static void addToList(Map<String, Contact> contactsMap,Contact newContact )

{

contactsMap.put(newContact.getLName(),newContact);

}

//deletes a contact from the list by last name

static void deleteFromList(Map<String, Contact> contactsMap,String pNo)

{

if(contactsMap.get(pNo)!=null)

{

System.out.println("Contact : "+contactsMap.get(pNo)+ " with phone number "+pNo+" is removed");

contactsMap.remove(pNo);

}

else

System.out.println("Contact does not exist with phone number "+pNo);

}

//Display all contacts

static void displyContacts(Map<String, Contact> contactsMap)

{

for (String key : contactsMap.keySet())

{

System.out.println(contactsMap.get(key));

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
* The Class ContactMap.
*/
public class ContactMap

{

   /**
   * The main method.
   *
   * @param args the arguments
   * @throws IOException Signals that an I/O exception has occurred.
   */
   public static void main(String args[]) throws IOException

   {

       Scanner input = new Scanner(System.in);

//Create a TreeMap , to store contact object with last name as the key

       Map<String, Contact> contactsMap = new TreeMap<String, Contact>();

//Prompt the user to enter the contacts file name

       System.out.print("Enter the contacts file name: ");

//Read the file name

       String contactsFile = input.nextLine();

//Open the file

       BufferedReader in = new BufferedReader(new FileReader(contactsFile));

       String line = "";

//Read all contacts into tree map

       while ((line = in.readLine()) != null)

       {

           String columns[] = line.split("\t");

           contactsMap.put(columns[1].trim(),
                   new Contact(columns[0].trim(), columns[1].trim(), columns[2].trim(), columns[3].trim()));

       }

//Run the menu

       while (true)

       {

           int choice;

//Print the menu

           System.out.println("1. Add a contact to the list");

           System.out.println("2. Delete a contact from the list");

           System.out.println("3. Display the entire contact list.");

           System.out.println("4.Exit");

           System.out.print("Enter your choice: ");

//Read the choice

           choice = input.nextInt();

           switch (choice)

           {

           case 1:

//Read new contact information

               System.out.print("Enter the first name: ");

               input.nextLine();

               String fName = input.nextLine();

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

               String lName = input.nextLine();

               System.out.print("Enter the phone number: ");

               String phoneNumber = input.nextLine();

               System.out.print("Enter the email-id: ");

               String eId = input.nextLine();

//Call the addToList method

               addToList(contactsMap, new Contact(fName, lName, phoneNumber, eId));

               break;

           case 2:

//Prompt the user to enter the last name

               System.out.print("Enter the last name to remove from the contacts: ");

               input.nextLine();

//Read the last name

               String phNo = input.nextLine();

//Call the deleteFromList method

               deleteFromList(contactsMap, phNo);

               break;

           case 3:

//Call displayContacts to print all contacts

               displyContacts(contactsMap);

               input.nextLine();

               break;

           case 4:

//Exit from the program

               System.exit(0);

           default:
               System.out.println("Enter a valid choice");

           }

       }

   }

//Adds the new Contact to the treemap.

   /**
* Adds the to list.
*
* @param contactsMap the contacts map
* @param newContact the new contact
*/
static void addToList(Map<String, Contact> contactsMap, Contact newContact)

   {

       contactsMap.put(newContact.getLName(), newContact);

   }

//deletes a contact from the list by last name

   /**
* Delete from list.
*
* @param contactsMap the contacts map
* @param pNo the no
*/
static void deleteFromList(Map<String, Contact> contactsMap, String pNo)

   {

       if (contactsMap.get(pNo) != null)

       {

           System.out.println("Contact : " + contactsMap.get(pNo) + " with phone number " + pNo + " is removed");

           contactsMap.remove(pNo);

       }

       else

           System.out.println("Contact does not exist with phone number " + pNo);

   }

//Display all contacts

   /**
* Disply contacts.
*
* @param contactsMap the contacts map
*/
static void displyContacts(Map<String, Contact> contactsMap)

   {

       for (String key : contactsMap.keySet())

       {

           System.out.println(contactsMap.get(key));

       }

   }

}

/**
* The Class Contact.
*/
class Contact{
   private String firstName;
   private String lastName;
   private String phoneNumber;
   /** The Id. */
   private String Id;
  
   public Contact(String aFirstName, String aLastName, String aPhoneNumber, String aId) {
       super();
       firstName = aFirstName;
       lastName = aLastName;
       phoneNumber = aPhoneNumber;
       Id = aId;
   }
  
   public String getFName() {
       return firstName;
   }
   public String getLName() {
       return lastName;
   }
   public String getPhoneNumber() {
       return phoneNumber;
   }
   public String getId() {
       return Id;
   }
   public void setFirstName(String aFirstName) {
       firstName = aFirstName;
   }
   public void setLastName(String aLastName) {
       lastName = aLastName;
   }
   public void setPhoneNumber(String aPhoneNumber) {
       phoneNumber = aPhoneNumber;
   }
   public void setId(String aId) {
       Id = aId;
   }
   @Override
   public String toString() {
       return "Contact [firstName=" + firstName + ", lastName=" + lastName + ", phoneNumber=" + phoneNumber + ", Id="
               + Id + "]";
   }
  
  
}

// The contact class is missing because of that your getting the error

Add a comment
Know the answer?
Add Answer to:
Hello Can you help to fix the program. When running, it still show Exception in thread...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java need help with searching for a contact Question:Write a program that uses an ArrayList of...

    Java need help with searching for a contact Question:Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact’s first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Your database program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Can you help me rearrange my code to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT