Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I created a \"phone directory\" java program where I can read \"raw data directl

ID: 3700967 • Letter: I

Question

I created a "phone directory" java program where I can read "raw data directly from original text file, add contact to textfile, and print out a file with the list sorted in order.. heres my code:

import java.util.*;
import java.io.*;

public class contactBook
{
   public static void main(String[] args)throws IOException
   {
       Scanner keyboard = new Scanner(system.in);
      
  
      
       System.out.println("1. Read contact list");
       System.out.println("2. Add contact");
       System.out.println("3. Print all contacts");
       System.out.println("4. Close program");

       String choice;
       do {
       choice = in.nextLine();
       switch (choice) {
       case "1":
       readList();
       break;
       case "2":
       addContact();
       break;
       case "3":
       printList();
       break;
       case "4":
       System.exit(0);
       break;
       default:
       System.out.println("Enter numer from 1 to 4");
       }
                   while(!choice.equals("4"));
          
       }
          
           public static void readList()
           {
           String outputFile = "contact.txt";  
       }
       public static void addContact()
       {
           BufferedWriter add = new BufferedWriter(new FileWriter("contact.txt"));
           add.write("Enter new contact information:");
           add.newLine();
       }
       public static printList()
       {
           BufferedReader reader = new BufferedReader(new FileReader("clist.txt"));
           String input;
           ArrayList<String> lines = new ArrayList<String>();
      
           while((input = reader.readLine()) != null)
           {
               lines.add(input);
           }
           // name of file reader for songs.txt
           file.close();
      
           //write new text file with songs sorted
           //collections.. group of classes that holds a collection of objects.. using merge sort
           Collections.sort(lines);
      
           System.out.println("LIST OF CONTACTS IN ORDER: ");
      
           FileWriter fileW = new FileWriter(outputFile);
           PrintWriter out = new PrintWriter(fileW);
               for (String outputLine : lines)
               {
          
                   System.out.println(outputLine);
               }
          
               // flush used to write any data stored in the out buffer. If you end a program without flushing a buffer, you could lose data
               // name of printwriter file
               out.flush();
               out.close();
               fileW.close();
       }
}}       there are some syntax errors, I would just like help on how to fix them... thank you in advance..

Explanation / Answer

Please find the program below.

Here we are assuming these facts -

1. Contact lists would be added to "contact.txt" file.

2. Contacts would be read from contact.txt file when "option 3" would be called and after sorting the contacts the same would be added to "contactList.txt" file. So, note that the unsorted contact list would be stored in "contact.txt" file whereas the sorted contact list would be stored in "contactList.txt" file.

JAVA PROGRAM

===============

OUTPUT

========

1. Read contact list
2. Add contact
3. Print all contacts
4. Close program
2
Enter new contact information:
XXX
2
Enter new contact information:
AAA
2
Enter new contact information:
PPP
2
Enter new contact information:
DDD
3
LIST OF CONTACTS IN ORDER:

AAA
DDD
PPP
XXX

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote