Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java bug while deleting a contact from a variable with error "it does exist or has not been declared"

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java bug while deleting a contact from a variable with error "it does exist or has not been declared"

    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.
    Last edited by Deep_4; November 7th, 2012 at 10:43 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Java Phonebook Assignment (Small Bugs)

    Hello Dawson, welcome to the Java Programming Forums

    Rather than posting those instructions to get the zip file, it would of been easier to post the direct download link!

    I'm sure the majority of members will be using this version of Java anyway.

    Looking at this code, I am going to need the hsa.Console and hsa.PhoneBook package. Please include this in your next post.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM
  2. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  3. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM
  4. [SOLVED] Problem with Grading calculator in java program
    By Peetah05 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: March 28th, 2009, 04:25 PM
  5. Create Phonebook using inheritance
    By islandGirl in forum Object Oriented Programming
    Replies: 5
    Last Post: February 17th, 2009, 04:42 AM