I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also this program has more then one class and also uses an interface. Right now I am working on
ArrayBasedPD class. I am trying to write a code for the remove method (line 158) that allows the user to enter a name, once the program sees that the name is in the directory, it deletes it. The way I tried to do it was simply do the opposite of the Add method, instead of incrementing the size of the array, I would decrement it, because I would believe that when decrementing the size that an entry is being deleted.
This may be kinda confusinf to understand since their are more Classes for this program, such as the Class phone directory is an interface that is being implemented by theArrayBasedPD class (this one). - package CH01;
-
-
import java.io.*;
-
-
/** This is an implementation of the PhoneDirectory interface that uses
-
* an array to store the data.
-
* @author Koffman & Wolfgang
-
*/
-
-
public class ArrayBasedPD implements PhoneDirectory {
-
-
// Data Fields
-
-
/** The initial capacity of the array */
-
private static final int INITIAL_CAPACITY = 100;
-
-
/** The current capacity of the array */
-
private int capacity = INITIAL_CAPACITY;
-
-
/** The current size of the array (number of directory entries) */
-
private int size = 0;
-
-
/** The array to contain the directory data */
-
private DirectoryEntry[] theDirectory =
-
new DirectoryEntry[capacity];
-
-
/** The data file that contains the directory data */
-
private String sourceName = null;
-
-
/** Boolean flag to indicate whether the directory was
-
modified since it was either loaded or saved. */
-
private boolean modified = false;
-
-
/** Method to load the data file.
-
pre: The directory storage has been created and it is empty.
-
If the file exists, it consists of name-number pairs
-
on adjacent lines.
-
post: The data from the file is loaded into the directory.
-
@param sourceName The name of the data file
-
*/
-
public void loadData(String sourceName) {
-
// Remember the source name.
-
this.sourceName = sourceName;
-
try {
-
// Create a BufferedReader for the file.
-
BufferedReader in = new BufferedReader(
-
new FileReader(sourceName));
-
String name;
-
String number;
-
-
// Read each name and number and add the entry to the array.
-
while ( (name = in.readLine()) != null) {
-
// Read name and number from successive lines.
-
if ( (number = in.readLine()) == null) {
-
break; // No number read, exit loop.
-
}
-
// Add an entry for this name and number.
-
add(name, number);
-
}
-
-
// Close the file.
-
in.close();
-
}
-
catch (FileNotFoundException ex) {
-
// Do nothing — no data to load.
-
return;
-
}
-
catch (IOException ex) {
-
System.err.println("Load of directory failed.");
-
ex.printStackTrace();
-
System.exit(1);
-
}
-
}
-
-
/** Add an entry or change an existing entry.
-
@param name The name of the person being added or changed
-
@param number The new number to be assigned
-
@return The old number or, if a new entry, null
-
*/
-
public String addOrChangeEntry(String name, String number) {
-
String oldNumber = null;
-
int index = find(name);
-
if (index > -1) {
-
oldNumber = theDirectory[index].getNumber();
-
theDirectory[index].setNumber(number);
-
}
-
else {
-
add(name, number);
-
}
-
modified = true;
-
return oldNumber;
-
}
-
-
/** Look up an entry.
-
@param name The name of the person
-
@return The number. If not in the directory, null is returned
-
*/
-
public String lookupEntry(String name) {
-
int index = find(name);
-
if (index > -1) {
-
return theDirectory[index].getNumber();
-
}
-
else {
-
return null;
-
}
-
}
-
-
/** Method to save the directory.
-
pre: The directory has been loaded with data.
-
post: Contents of directory written back to the file in the
-
form of name-number pairs on adjacent lines.
-
modified is reset to false.
-
*/
-
public void save() {
-
if (modified) { // If not modified, do nothing.
-
try {
-
// Create PrintWriter for the file.
-
PrintWriter out = new PrintWriter(
-
new FileWriter(sourceName));
-
-
// Write each directory entry to the file.
-
for (int i = 0; i < size; i++) {
-
// Write the name.
-
out.println(theDirectory[i].getName());
-
// Write the number.
-
out.println(theDirectory[i].getNumber());
-
}
-
-
// Close the file and reset modified.
-
out.close();
-
modified = false;
-
}
-
catch (Exception ex) {
-
System.err.println("Save of directory failed");
-
ex.printStackTrace();
-
System.exit(1);
-
}
-
}
-
}
-
-
/** Find an entry in the directory.
-
@param name The name to be found
-
@return The index of the entry with the requested name.
-
If the name is not in the directory, returns -1
-
*/
-
private int find(String name) {
-
for (int i = 0; i < size; i++) {
-
if (theDirectory[i].getName().equals(name)) {
-
return i;
-
}
-
}
-
return -1; // Name not found.
-
}
-
/**remove Entry
-
* @param name The name of the person to be deleted
-
*
-
*/
-
public void remove(String name) {
-
int i = find(name);
-
if(i <= -1)
-
System.out.println("Entry not found; cannot delete!");
-
else
-
theDirectory[size] = new DirectoryEntry(name);
-
size--;
-
-
modified = true;
-
}
-
-
-
-
/** Add an entry to the directory.
-
@param name The name of the new person
-
@param number The number of the new person
-
*/
-
private void add(String name, String number) {
-
if (size >= capacity) {
-
reallocate();
-
}
-
theDirectory[size] = new DirectoryEntry(name, number);
-
size++;
-
}
-
-
/** Allocate a new array to hold the directory. */
-
private void reallocate() {
-
capacity *= 2;
-
DirectoryEntry[] newDirectory = new DirectoryEntry[capacity];
-
System.arraycopy(theDirectory, 0, newDirectory, 0,
-
theDirectory.length);
-
theDirectory = newDirectory;
-
}
-
-
-
}
6 4226
Looks like you got some code from somewhere that you don't really understand what it does.
Better start with Sun's Java tutorial.
That remove() method doesn't make sense to me: -
public void remove(String name) {
-
int i = find(name);
-
if(i <= -1)
-
System.out.println("Entry not found; cannot delete!");
-
else
-
theDirectory[size] = new DirectoryEntry(name);
-
size--;
-
-
modified = true;
-
}
-
I read it as: try to find that name; if not found print a message otherwise make a new entry? and decrement the size of it all?
kind regards,
Jos
Ok if that is what is wrong, then How can I make it delete the entry? I thought decrementing the size of the array would be deleting an entry out of the array. Or do I just decrement the size? Im confused.
@falconsx23
For starters: you can't resize an array once you have instantiated one. Better use an ArrayList, you can delete any element from it given the index value of the element.
kind regards,
Jos
Well besides making an array list, is there anyway that I can remove an entry. I was thinking of another idea where I could overwrite an element in the array. Like it searches for a name, once the array is found, it takes the name before the found name and replaces moves it up. For instance:
Bob, Ray, Jill
Ray was found, but the remove method allows Bob to replace Ray, thus making it
Bob, Bob, Jill
@falconsx23
The ArrayList class object can do that for you with its 'remove()' method; read the API documentation.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: joemono |
last post by:
Hello everyone!
First, I appologize if this posting isn't proper "netiquette" for this
group.
I've been working with perl for almost 2 years now. However, my regular
expression knowledge is...
|
by: William Morris |
last post by:
Our application tracks contact information. One of our clients, a car
dealership, has asked about being able to enter a lastname and phone number
and getting as much of the main form filled out as...
|
by: Danny Yeadon |
last post by:
Hi
I need to remove unwanted characters from phone numbers from my phone bill
to analyse the data.
Some examples are
02 48222222 i need to remove the space
+61266667656 ...
|
by: htperth |
last post by:
Hi all,
Note that I'm new to LDAP and Active Directory and am writing an application that retrieves a simple phone list for all the users in our domain. So far I have the following console app...
|
by: htperth |
last post by:
Hi all,
Note that I'm new to LDAP and Active Directory and am writing an application that retrieves a simple phone list for all the users in our domain. So far I have the following console app...
|
by: homevista |
last post by:
PART III: Putting things together
In part I we examined the modem to verify that it supported voice. If so, we took a note about the voice data format that we would use. In the second part, we...
|
by: ryushinyama |
last post by:
I am wanting to remove 1's that Canadian customers put in front of their numbers because when FedEx imports them for shipping they leave the 1 and cut off the last number. Other countries numbers...
|
by: Blue Streak |
last post by:
Hello, Folks!
Does anyone know of a website that lists the local phone number
formats for each country?
TIA...
|
by: Propoflady |
last post by:
My contacts can have as many as five or six phone numbers - is it
possible to make a query that puts the name, and each phone number
after it on the same line - for this reason I have two tables -...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |