Question

Java Your boss has just put you in charge of updating the company's client contact list....

Java Your boss has just put you in charge of updating the company's client contact list.

This file contains records in the following format: id,first_name,last_name,email

1,Norry,Killby,nkillby0@photobucket.com

Your job is to reformat each record as follows:

last_name, first_name, email

Killby, Norry, <nkillby0@photobucket.com>

Starter Code:

package contactlistupdater;

import java.io.FileNotFoundException;

import java.io.IOException; import java.nio.file.Paths; import java.util.Formatter; import java.util.Scanner; import sun.reflect.generics.reflectiveObjects.NotImplementedException; public class ContactListUpdater { private static final String INPUT_FILENAME = "contacts.txt"; private static final String OUTPUT_FILENAME = "updated-contacts.txt"; /** * Transforms a String containing a contact entry to a new format */ public static String processContactEntry(String input) { // TODO your code here... return input; } public static void main(String[] args) { // Open input/output files and process line by line try (Formatter output = new Formatter(OUTPUT_FILENAME)) { try (Scanner input = new Scanner(Paths.get(INPUT_FILENAME))) { // Process each line while (input.hasNextLine()) { output.format("%s%n", processContactEntry(input.nextLine())); } } catch (IOException ex) { System.err.println("Unable to open input file: " + INPUT_FILENAME); ex.printStackTrace(); } } catch (FileNotFoundException ex) { System.err.println("Unable to open output file: " + OUTPUT_FILENAME); ex.printStackTrace(); } } }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THE CODE FOR THE ABOVE PROBLEM IS:-

import java.io.FileNotFoundException;

import java.io.IOException;

import java.nio.file.Paths;

import java.util.Formatter;

import java.util.Scanner;

// import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class ContactListUpdater {

    private static final String INPUT_FILENAME = "contacts.txt";

    private static final String OUTPUT_FILENAME = "updated-contacts.txt";

    /** * Transforms a String containing a contact entry to a new format */

    public static String processContactEntry(String input) {

        //splitting the sentence by comma into a string array

        String arr[] = input.split(",");

        String str = "";

        // adding the result to a string str in the resulted format

        str = arr[2] + "," + arr[1] + ",<" + arr[3] + ">";

        return str;

    }

    public static void main(String[] args) {

        // Open input/output files and process line by line

        try (Formatter output = new Formatter(OUTPUT_FILENAME)) {

            try (Scanner input = new Scanner(Paths.get(INPUT_FILENAME))) { // Process each line

                while (input.hasNextLine()) {

                    output.format("%s%n", processContactEntry(input.nextLine()));

                }

            } catch (IOException ex) {

                System.err.println("Unable to open input file: " + INPUT_FILENAME);

                ex.printStackTrace();

            }

        } catch (FileNotFoundException ex) {

            System.err.println("Unable to open output file: " + OUTPUT_FILENAME);

            ex.printStackTrace();

        }

    }

}

FOR BETTER UNDERSTANDING PLEASE GO THROUGH THE SCREENSHOT BELOW:-

NOW INPUT AND OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Java Your boss has just put you in charge of updating the company's client contact list....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java write a simple 1-room chat server that is compatible with the given client code.

    In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...

  • Java need help with searching for a contact Question:Write a program that uses an ArrayList of...

    Java need help with searching for a contact Question:Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact’s first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Your database program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and...

  • Modify your program in Lab Assignment 4A to throw an exception if the file does not...

    Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result.   Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. My code: import java.io.*; import java.util.*; public class Lab4B { public static void main(String[] args) throws IOException { final String input_file = "Lab_4A.txt"; final String output_file = "Lab_4A.txt"; Scanner x = null; FileWriter y = null; try {...

  • Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...

    Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • This is a JAVA language The files provided in the code editor to the right contain...

    This is a JAVA language The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Note: DebugData2.txt does not need to be edited. It is used by the program. DebugData2.txt 435-9845 239-9845 981-9883 384-5656 875-3784 874-8120 DebugThirteen2.java // Program reads in a file of phone numbers without area codes // inserts "(312)...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File;...

    Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Lab8Num1 { public static void main(String[] args) { //Declaring variable to be used for storing and for output double biggest,temp; //Creating file to read numbers File inFile = new File("lab8.txt"); //Stream to read data from file Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { //Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex); } //get first number...

  • import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private...

    import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private static final String OUTPUT_FILE = "crimes.txt"; public static void main(String[] args) { int totalCrimes = 0; // read all the rows from the csv file and add the total count in the totalCrimes variable try { Scanner fileScanner = new Scanner(new File(INPUT_FILE)); String line = fileScanner.nextLine(); // skip first line while (fileScanner.hasNext()) { String[] tokens = fileScanner.nextLine().split(","); if (tokens.length == 4) { totalCrimes +=...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT