I am having some errors with the code that I made which is supposed to be a phonebook that stores names, and numbers and that also allows the user to add a contact, search for a contact, edit contact info, delete a contact, and exit the program.
So far all is pretty much well. I have the majority of my code up and running, however, on the bottom on step 2 of deleting a contact I am recieving errors for the variable Cname saying that it does exist or has not been declared when it has been at the starting of the code. It also says one of my methods b.Search wasn't found in type Phonebook class.
Please take a look.
// The "A7DawsonMerkleyPhonebook" class. import java.awt.*; import hsa.Console; import hsa.PhoneBook; public class A7DawsonMerkleyPhoneBook { static Console c; static PhoneBook b; public static void main (String[] args) { c = new Console ("Dawson's Phonebook Application"); b = new PhoneBook (); // Commands for phonebook actions PhoneMenu (); } public static void PhoneMenu () // Declare Method PhoneMenu { c.clear (); // Clears the screen so that the Phonebook Menu can be displayed. c.println ("Welcome to Dawson's PhoneBook.\n"); c.println ("Welcome to the main menu."); c.println ("Please enter a number between 1 - 5. Based on whichever action you want to do"); c.println ("Please enter the number assigned to it."); c.println ("Dawson's Phonebook personal menu:\n"); c.println ("1. Add a Contact: Adds a name of a contact and their phone number to your phonebook."); c.println ("2. Search for a Contact: Searches for the name of a contact."); c.println ("3. Delete a Contact: Removes an entry from the phonebook."); c.println ("4. Edit Contact information: Changes an entry from the phonebook."); c.println ("5. Exit Phonebook application."); // Displays the main menu and selection of different actions the phonebook can preform. int user; // Declares int user int decision = c.readInt (); // This switch registers the user's number inputed based on which action they wanted to preform. // This switch then takes this input and register's it with a certain case depending on what number the user enters // so that the program can be run correctly. switch (decision) { case 1: Add (); // Assigns method Add to Case 1. break; case 2: Search (); // Assigns method Search to Case 2. break; case 3: Delete (); // Assigns method Delete to Case 3. break; case 4: Edit (); // Assigns method Edit to Case 4. break; case 5: Exit (); // Assigns method Exit to Case 5. break; } PhoneMenu (); // Once the user's input is stored make the main menu appear. } public static void Add () //Creates a new Method to store data and commands entered by the user that are used in the add action. { c.clear (); // Clears screen c.println ("1. Please enter the name of the Contact."); //Ask user for contact name. String Cname = c.readLine (); //Store the name of the Contact in the variable name as the contact name. c.println ("2. Please enter the number for the Contact."); String Cnumber = c.readLine (); // Read the phone number, store in Cnumber string. b.add (Cname, Cnumber); // Stores names in phonebook class c.println ("/tThankyou for entering the Contact's Name."); c.println ("/tPlease enter any key to return to the main menu."); c.readLine (); // Registers the user's keystroke in order to return to the main menu } public static void Search () //Creates a new used to search for a registered Contact and their number. { c.clear (); // Clears Screen. c.println ("Please Enter the name of the Contact you are searching for."); String Cname = c.readLine (); //Stores the name of the contact to bring back the contacts name and phonebook. String Cnumber = b.Search (Cname); // If user enters a wrong name display message "Error". if (Cnumber.equals ("")) { c.println ("Searching Error."); } else //If they enter the right answer then display the Contact name and Number { c.println ("Name: " + Cname); c.println ("Phone Number:" + Cnumber); } c.println ("/tPlease enter any key to return to the main menu."); c.readLine (); // Registers the user's keystroke in order to return to the main menu. } public static void Delete () // Create a Delete Method { c.clear (); // Clears the Screen. c.println ("1. Please enter the name of a contact you want to delete."); String Cname = c.readLine (); //Store the name of the Contact entered by user boolean exists = !b.lookUp (Cname).equals (""); //Checks to see if Contact is registered in phonebook. if (exists) //If contact exists remove them from the phonebook. { b.remove (Cname); // Call the remove method from phonebook class. c.println ("You have deleted the selected Contact."); } else //Else, display message saying contact does not exist. { c.println ("Contact is not registered with phonebook."); } c.println ("Please enter any key to return to the main menu."); c.readLine (); //Please enter any key to return to the main menu } public static void Edit () { c.clear (); //Clear the screen. c.println ("__________________________________________________________\n"); c.println ("1. Please enter the name of the Contact you want to \nmodify."); String Cname = c.readLine (); //Stores name that user enters. boolean exists = !b.lookUp (Cname).equals (""); //Create a Boolean expression in order to see if they if (exists) { b.remove (Cname); // Call the remove method from phonebook class. } else { c.println ("Sorry, the contact you entered does not exist"); c.readLine (); return; } } { c.println ("2. Please enter the new phone number for " + Cname + "."); String Cnumber = c.readLine (); // Read the new phone number, store in string. // Add the entry to the phonebook with the same name as before, but with // a new number. This simulates modifying the entry. b.add (Cname, Cnumber); c.println ("Please enter any key to return to the main menu."); c.readLine (); // The user presses any key to return to the menu. } public static void Exit () { System.exit (1); } }
Thanks!
To download our schools Java System please go to Mayfield Secondary School
1. Once you have loaded this website you will see a series of options at the top of the screen, one of these options will read "Student", Put your mouse over this option, it will then open up to a list with several different options.
2. Choose the option "Teachers pages". It should send you to a website with a list of teacher names and courses.
3. Look for the teacher and course code Jone (Mr) ICS3M : Grade 11 Computer Science, click this link.
4. It will open up to a website for all of our school assignments and lesson plans, there is also a link to download Java v 1.71.
5. If you look on the far right side of the screen their should be a menu called "support resources" under this menu you will see a ZIP file called ready171_install. This is our version of Java that we are use. My code should run properly in this version of java.
------------ MY homework assignment is called ICS3M Methods. ----------------------- (You can look here to see what we have to have in our program.