Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 lines = new ArrayList(); 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..

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

What is the effect of word war second?

Answered: 1 week ago