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 8 of 8

Thread: School Assignment; Issue with lookUp method

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

    Default School Assignment; Issue with lookUp method

    Hi everyone, I'm taking a intro to programming in Java class at school, and we have an assignment requiring us to take the already provided hsa.Phonebook class from Ready To Program Java, and upgrading it with more methods. I've got a good amount so far, but unfortunately when the version I am about to post is compiled and executed, the lookUp function isn't quite coded correctly.

    I'll let you all look at it for yourself, and tell me what you think.

    The Custom_Phonebook class (I.E. The main user program)

    // The "Custom_PhoneBook" class.
    import java.awt.*;
    import hsa.Console;
    import hsa.Stdin;
     
    public class Custom_PhoneBook
    {
        static Console c;           // The output console
     
        public static void main (String[] args)
        {
            String userName; //Sets userName as a String Variable
            String entryName = (""); //Sets entryName as a String Variable with the value of blank
            String firstName = (""); //Sets firstName as a String Variable with the value of blank
            String lastName = (""); //Sets lastName as a String Variable with the value of blank
            String phoneNumber = (""); //Sets phoneNumber as a String Variable with the value of blank
            String address = ("");  //Sets address as a String Variable with the value of blank
            String email = ("");    //Sets email as a String Variable with the value of blank
            boolean loop = true;
     
            //Greets user, asks for user's name
            System.out.print ("Welcome to your Personal Phonebook! What's your name? ");
            userName = Stdin.readString ();
            MyPhoneBook pb = new MyPhoneBook ();
     
            String[] temp;
            String[] [] data;
     
            while (loop = true)
            {
                System.out.println ("1. New Entry");
                System.out.println ("2. Edit Number");
                System.out.println ("3. Look Up Name");
                System.out.println ("4. Delete Entry");
                System.out.println ("5. Quit Program");
                System.out.println ("");
                System.out.print ("Alright then, " + (userName) + ". Please choose a option from the menu above. ");
     
                int menuChoice = Stdin.readInt ();
                switch (menuChoice)
                {
                    case 1:
                        System.out.println ("");
                        System.out.println ("You chose to make a new entry.");
                        System.out.print ("Please input the first name for your new entry. ");
                        entryName = Stdin.readString ();
                        temp = pb.lookUp (entryName);
                        if (!temp[0].equals (""))
                        {
                            System.out.println ("Sorry, you've already put a contact under that first name.");
                            System.out.println ("");
                        }
                        else
                        {
                            firstName = entryName;
                            System.out.print ("Please input the last name for your new entry. ");
                            entryName = Stdin.readString ();
                            temp = pb.lookUp (entryName);
                            if (!temp[1].equals (""))
                            {
                                System.out.println ("Sorry, you've already put a contact under that last name.");
                                System.out.println ("");
                            }
                            else
                            {
                                lastName = entryName;
                                System.out.print ("Please input the number for your new entry. ");
                                phoneNumber = Stdin.readString ();
                                System.out.print ("Please input the address for your new entry. ");
                                address = Stdin.readString ();
                                System.out.print ("Please input the email address for your new entry. ");
                                email = Stdin.readString ();
                                pb.add (firstName, lastName, phoneNumber, address, email);
                                System.out.println ("");
                            }
                        }
                        break;
     
                    case 2:
                        System.out.println ("");
                        System.out.println ("You chose to edit a number.");
                        System.out.println ("Please input the name of the person who's number ");
                        System.out.print ("you are editing. ");
                        entryName = Stdin.readString ();
                        temp = pb.lookUp (entryName);
                        pb.remove (firstName, lastName, phoneNumber, address, email);
                        System.out.print ("Please input the first name for your new entry. ");
                        firstName = Stdin.readString ();
                        System.out.print ("Please input the last name for your new entry. ");
                        lastName = Stdin.readString ();
                        System.out.print ("Please input the new number. ");
                        phoneNumber = Stdin.readString ();
                        System.out.print ("Please input the address for your new entry. ");
                        address = Stdin.readString ();
                        System.out.print ("Please input the email address for your new entry. ");
                        email = Stdin.readString ();
                        pb.add (firstName, lastName, phoneNumber, address, email);
                        System.out.println ("");
                        break;
     
                    case 3:
                        System.out.println ("");
                        System.out.println ("You chose to look up a name in your Phonebook.");
                        System.out.println ("Please input the name of the person you've ");
                        System.out.print ("entered, and use proper capitilization. ");
                        entryName = Stdin.readString ();
                        temp = pb.lookUp (entryName);
                        System.out.println ("");
                        System.out.println ("Name: " + (temp [0]) + " " + (temp [1]) + "");
                        System.out.println ("Number: " + (temp [2]) + " Address: " + (temp [3]) + " email: " + (temp [4]) + "");
                        System.out.println ("");
                        break;
     
                    case 4:
                        System.out.println ("");
                        System.out.println ("You chose to delete an entry.");
                        System.out.print ("Please input the name of the user you want to delete. ");
                        entryName = Stdin.readString ();
                        temp = pb.lookUp (entryName);
                        pb.remove (firstName, lastName, phoneNumber, address, email);
                        System.out.println ("");
                        break;
     
                    case 5:
                        System.exit (0);
                        break;
     
                }
            }
     
     
            // Place your program here.  'c' is the output console
        } // main method
    } // Custom_PhoneBook class

    The MyPhoneBook class (I.E. Where all the upgraded methods are.)

    public class MyPhoneBook
    {
        int numEntries;
        String data[] [];
        String temp[];
        String entryName;
     
        public MyPhoneBook ()
        {
            numEntries = 0;
            data = new String [100] [5];
            temp = new String [5];
            temp [0] = "";
            temp [1] = "";
        } // PhoneBook constructor
     
     
        public void add (String firstName, String lastName, String phoneNumber, String address, String email)
        {
            data [numEntries] [0] = firstName;
            data [numEntries] [1] = lastName;
            data [numEntries] [2] = phoneNumber;
            data [numEntries] [3] = address;
            data [numEntries] [4] = email;
            numEntries++;
        } // add method
     
        public String[] lookUp (String entryName)
        {
            for (int cnt = 0 ; cnt < numEntries ; cnt++)
            {
                if (!data [cnt] [0].equals (""))
                {
                    temp [0] = data [cnt] [0];
                    temp [1] = data [cnt] [1];
                    temp [2] = data [cnt] [2];
                    temp [3] = data [cnt] [3];
                    temp [4] = data [cnt] [4];
     
                    return temp;
                }
            }
            return temp;
        } // lookUp method
     
        // Delete the entry
        public void remove (String firstName, String lastName, String phoneNumber, String address, String email)
        {
            for (int cnt = 0 ; cnt < numEntries ; cnt++)
            {
                if (data [cnt] [0].equals (data [cnt] [0]))
                {
                    data [cnt] [0] = ("");
                    data [cnt] [1] = ("");
                    data [cnt] [2] = ("");
                    data [cnt] [3] = ("");
                    data [cnt] [4] = ("");
                    numEntries--;
                }
            }
        } // remove method
     
     
        /*    public void removeAll (String firstName, String lastName, String phoneNumber, String address, String email)
            {
                for (int cnt = 0 ; cnt < numEntries ; cnt++) //Loops following code while the count is less than the number of Contacts in the Phonebook
                {
                    if (data [cnt] [0].equals (data [cnt] [0]))
                    {
                        data [cnt] [0] = ("");
                        data [cnt] [1] = ("");
                        data [cnt] [2] = ("");
                        data [cnt] [3] = ("");
                        data [cnt] [4] = ("");
                        numEntries--;
                    }
                } // removeAll method */
    } // MyPhoneBook class

    If anyone can spot what I'm doing wrong here and push me in the right direction, it would be a big help. Thanks!

    - Zero
    Last edited by Zero_Starlight; May 21st, 2012 at 06:20 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: School Assignment; Issue with lookUp method

    What if the entry name simply isn't there?

    What's going wrong?

    Is it simply not compiling, is it throwing an ArrayIndexOutOfBoundsException, or is it working and compiling but somehow not giving the correct output?
    Last edited by javapenguin; May 21st, 2012 at 06:23 PM.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Assignment; Issue with lookUp method

    temp[2], temp[3], temp[4] are the phonenumber, address and email respectively. They aren't being checked, so Ready to Program doesn't seem to have any issues with it. I would like to inform you however, that I have updated my code, and have it fixed to the point where I can now add a total of one name. Please see above in just a second for the updated code.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Assignment; Issue with lookUp method

    I don't quite follow you. Do you mean if the user hasn't put a entry in yet? Or they leave it blank?

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Assignment; Issue with lookUp method

    My issue now is that the program will compile and run fine, however, I can only add one name. All names after this are rejected for being used already.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: School Assignment; Issue with lookUp method

    I'm not sure why you have the ( ) around the "" in the String definitions. I've never seen that before now but oddly it compiles.

    It would appear, and maybe I'm mistaken, I'm somehow having a hard time following your code, but anyway, it would seem that firstName = "" if it goes into the else statement.

    Hence, I think you're adding an entry with no first name.

    Also, unless I'm mistaken, your lastName is also = "" too.

    Hence your entry has no last name and no first name.

    Yes, lastName and firstName are never set, even if you are inputting their values in entryName.

    Hence you keep adding a guy with no first or last name every time and it's going to say that you already have somebody with that first or last name already in there after only 1 entry.
    Last edited by javapenguin; May 21st, 2012 at 06:58 PM.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Assignment; Issue with lookUp method

    Quote Originally Posted by javapenguin View Post
    I'm not sure why you have the ( ) around the "" in the String definitions. I've never seen that before now but oddly it compiles.

    It would appear, and maybe I'm mistaken, I'm somehow having a hard time following your code, but anyway, it would seem that firstName = "" if it goes into the else statement.

    Hence, I think you're adding an entry with no first name.

    Also, unless I'm mistaken, your lastName is also = "" too.

    Hence your entry has no last name and no first name.

    Yes, lastName and firstName are never set, even if you are inputting their values in entryName.

    Hence you keep adding a guy with no first or last name every time and it's going to say that you already have somebody with that first or last name already in there after only 1 entry.
    No, that's not it. The if statement checks to see if the firstName and lastName haven't been used yet in any of the array entries.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: School Assignment; Issue with lookUp method

    Yes, but it appears, and I traced the variables using my word find, that you are adding it and never setting the value of first or last name when you are calling add.

    I see no,

    lastName = Stdin.readString();

    So thus it would still be "" when you're adding it each time.

    To see for sure, I can't check as I don't have the Console class and I can't seem to import it to JGrasp and anyway, I think I'm certain of the problem, but

    add

    System.out.println("First Name: " + firstName);
    System.out.println("Last Name: " + lastName);

    before you call the add method to see if I'm right.

    I have traced the variable lastName in its uses:

    String lastName = (""); //Sets lastName as a String Variable with the value of blank Line 16
    entryName = lastName; Line 67

    pb.add (firstName, lastName, phoneNumber, address, email); Line 75

    You have lastName being read in for case 2, but that's for edit.

    It would appear that both add and edit cases are calling add method.

    Now I'm confused.
    Last edited by javapenguin; May 21st, 2012 at 08:08 PM.

Similar Threads

  1. Binary Search Tree Lookup Method
    By Sbonett in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2012, 07:43 AM
  2. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 13th, 2012, 08:22 AM
  3. School Assignment AHH!
    By Europa in forum Loops & Control Statements
    Replies: 8
    Last Post: January 20th, 2012, 09:19 AM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  5. 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