Question: Take the AddressBook program which you created and change the way it saves and reads back the addresses to utilize Object Serialization here's the code
Take the AddressBook program which you created and change the way it saves and reads back the addresses to utilize Object Serialization
here's the code i used
package addressbook; /** * java that saves the user's name, phone numbers and email address into a text file *
* then has the user enter their last name and has the program read back the persons contact info *
* @author * @since 3-2-21 * @version 1.0 * */
import java.io.*; import java.util.Scanner;
public class AddressBook { public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(new File("address.txt")); while(true){ /**asking the user to enter a record * */ System.out.println("Enter a record:(lastname firstName phoneNumber email)"); String line = sc.nextLine(); /**breaks the loop if user inputs eof * */ if(line.trim().equalsIgnoreCase("eof")) break; out.println(line); } out.close(); /**reads from the file * */ Scanner in = new Scanner(new File("address.txt")); /**asks the user to enter the name to search * */ System.out.print("Enter last name to search: "); String name = sc.nextLine(); while(in.hasNextLine()){ String readLine = in.nextLine(); if(readLine.split(" ")[0].equalsIgnoreCase(name)){ /**reads the name and phone * */ String firstName = readLine.split(" ")[1]; String lastName = readLine.split(" ")[0]; String phone = readLine.split(" ")[2]; /**printing the record details * */ System.out.println("Record found: "+firstName+" "+lastName+" "+phone); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
