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)
Code Java:
// 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.)
Code Java:
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
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?
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.
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?
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.
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.
Re: School Assignment; Issue with lookUp method
Quote:
Originally Posted by
javapenguin
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.
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.