Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me complete the following java programming tasks. I need a correct answer as a reference for me to correct my own tasks. Thank
Please help me complete the following java programming tasks. I need a correct answer as a reference for me to correct my own tasks. Thank you very much.
Here are the tasks in the exercise:
Checking for duplicated entry when a new phone number is addedno need to finish
Finding a phone number entry by nameno need to finish
Delete a phone number entry by nameplease finish this part
Here is a part of the uncompleted java code given in the task. Please finish this part:
Task Deleting a phone book entry by name
Get the path of the text file
Use a BufferedReader to read the data one by one
For each data line create a PhoneBookEntry
If the name in the PhoneBookEntry is NOT equal to the input name
add the entry to the entries array
Use a BufferedWriter to write the stored data from the array into
the text file one by one
Close the BufferedWriter
The main method to start the phone book program
public static void mainString args
Start the phone book program
PhoneBook phonebook new PhoneBook;
phonebook.mainmenu;
In this exercise, each phone number entry is still stored as a separate line. However, the name and the phone number of one entry are separated by a colon, without a space. You can see the format of the phone book from the given phonebook.txt as shown below:
CSO:
FMO:
FO:
HRO:
ITSC:
SAO:
The third task is deleting a phone number entry by name.
Since we only have one single file, you will need to read the phone book into a temporary storage, delete the appropriate entry and then save the data back to the same file. The temporary place that we are going to use is an array. You can find this code in the given deleteNumberByName method:
Prepare an array to store the phone number entries
PhoneBookEntry entries new PhoneBookEntry;
Store the number of entries in the phone book
int numOfEntries ;
The above code creates an array of PhoneBookEntry. The size of the array has been fixed at so we need something to remember the actual number of entries in the phone book. The numOfEntries variable is created for this purpose. It stores the actual number of entries in the phone book.
You will read the data file line by line. For each line in the file you will create a new PhoneBookEntry object and put the object in the entries array. You will need to store the phone book entry into the array using this code:
entriesnumOfEntries entry;
numOfEntries;
The first line of above code stores the PhoneBookEntry object into the array, using the position indicated by the value of numOfEntries. Initially the numOfEntries variable is That means the first time the above code is run, the PhoneBookEntry object becomes the first item of the array. When you run it the second time, you should store the PhoneBookEntry object in the second position of the array. Therefore, each time after you store the object in the array you need to increase the numOfEntries variable by one, as shown in the second line of the above code. If you do this, the numOfEntries variable will keep track of the current item you want to store the object as well as the actual number of items you have stored so far.
You don't actually need to store all phone number entries into the entries array. Remember that the objective of this task is to delete one entry from the phone book, you can store all entries in the array, except the one which matches the name entered by the user. You can use an if statement to do this, as shown below:
if entry.getNameequalsname
Store the object in the array...
After this stage, the entries array should contain all phone number entries, except the one you want to delete.
You can save the entries back to the data file. You can use a BufferedWriter to do that.
To get a BufferedWriter, you can use the following line of code assuming you have a correct Path object:
BufferedWriter writer
Files.newBufferedWriterpath Charset.defaultCharset StandardOpenOption.APPEND;
However, you need to use a slightly different BufferedWriter this time. That means you need to remove the StandardOpenOption.APPEND parameter so that the BufferedWriter will erase the current content of the file before writing the new one.
If the BufferedWriter is ready, you can use a for loop to write the entries into the file. For each entry you need to write the content of the entry into the file using the following code, assuming entriesi points to the current entry in the array inside the loop:
Add a phone number entry in a line
writer.writeentriesitoLine;
writer.newLine;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started