Question: JAVA Assume you work for a company that tracks customer information, including name, gender and phone numbers Your company has a file called customers.txt which
JAVA Assume you work for a company that tracks customer information, including name, gender and phone numbers
Your company has a file called customers.txt which contains the following information:
Jiming Wu F 4082123458 James Brown M 8315678432 Leanna Perez F 4087654433 Xing Li M 8313214555 Stacey Cahill O 8312123333 Mohammed Abbas M 4083134444 Kumari Chakrabarti F 4086667777 Shakil Smith M 4082123333 Jung Ahrin F 8319257788 Pedro Martinez M 4086162323 Ally Gu O 4089256776 Tamara White F 8317778978 Alvin Ngo M 4089256677 Abir Fadel M 8316645325 Brad Feinman M 8312023443 Xiaohang Yue M 8318990033
Your task is to read in the data from the above file using a loop, format the data, and write it into a new file called contacts.txt
When you are finished, contacts.txt should contain the following information:
Ms. Jiming Wu Phone: (408) 821-2123 Mr. James Brown Phone: (831) 156-5678 Ms. Leanna Perez Phone: (408) 876-7654 Mr. Xing Li Phone: (831) 132-3214 Mx. Stacey Cahill Phone: (831) 121-2123 Mr. Mohammed Abbas Phone: (408) 831-3134 Ms. Kumari Chakrabarti Phone: (408) 866-6667 Mr. Shakil Smith Phone: (408) 821-2123 Ms. Jung Ahrin Phone: (831) 192-9257 Mr. Pedro Martinez Phone: (408) 861-6162 Mx. Ally Gu Phone: (408) 892-9256 Ms. Tamara White Phone: (831) 177-7778 Mr. Alvin Ngo Phone: (408) 892-9256 Mr. Abir Fadel Phone: (831) 166-6645 Mr. Brad Feinman Phone: (831) 120-2023 Mr. Xiaohang Yue Phone: (831) 189-8990
In other words, whenever the customers.txt file contains an M below the customer name, the contacts.txt file should have the word Mr. before the name.
And, whenever the customers.txt file contains an F below the customer name, the contacts.txt file should have the word Ms. before the name.
Finally, whenever the file contains an O below the customer name, the contacts.txt file should have the word Mx. before the name.
In addition, the unformatted phone numbers in customers.txt must be formatted as (XXX) XXX-XXXX inside of contacts.txt.
Array requirements
You are required to store the names and phones in two separate - but parallel - arrays. In other words, the same index in each array should store the information about one unique customer. For example, index 0 in the names array should store the name of Jiming Wu and index 0 in the phones array should store her phone number.
You can assume for the purpose of the program that your arrays are both of length 16 when you declare them.
Method requirements
formatPhones Method:
The method is named formatPhones
It takes in an array of Strings as a parameter (unformatted phone numbers)
It alters each String to be a properly formatted phone number (XXX) XXX-XXXX
It cannot call any outside methods that we have not discussed in this class.
It returns nothing.
printArrays Method:
The method is named printArrays
It throws IOException
It takes in three parameters:
the first and second parameters are arrays of Strings,
the third parameter is a String for the name of a text file in which to write out the data
The method must open the file whose name is passed in as a parameter
It declares a PrintWriter and uses it to write to the specified file
It uses a for loop to print out the contents of the array in the file, with each element on its own line, in the following format:
names[i]
Phone: phones[i]
It then closes the PrintWriter.
It returns nothing.
Copy and paste the starter code into a new class file called PhoneContacts.java:
/* * @author * CIS 36A */ import java.io.*; import java.util.Scanner; public class PhoneContacts { public static void main(String[] args) throws IOException { String name, gender, phone;
//Declare your two arrays here - one for names and one for phones
//Declare File and Scanner
//Below variable to keep track of array index in loop
int i = 0; while(??????) {
//read in name and gender
//if statements related to gender
//read in phone
//store phone number in phones array
}
//call formatPhones method
//call printArrays method
//close Scanner
}
//write Javadoc comment here
public static void formatPhones(String[] phones) {
return;
}
//write Javadoc comment here
public static void printArrays(String[] names, String[] phones, String fileName){//throw IOException return; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
