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
Half of the 16 customers in this file have a phone number with an 831 area code.
Half have a phone numbers have a 408 area code.
Write a program that reads in all of the customer information using a loop, and stores the names of the customers in two different string arrays - each array of length 8.
One array should store the customers with a 408 area code and one array should store the customers with an 831 area code.
Hint: Use substr to determine the area code of the customer's phone number.
When the customers are stored in each array, they should be stored as Mr., Ms. or Mx. depending on the gender below their name in the file.
In other words, whenever the customers.txt file contains an M below the customer name, you should place the word Mr. before the name.
Whenever the customers.txt file contains an F below the customer name, you should place the word Ms. before the name.
Finally, whenever the file contains an O below the customer name, you should place the word Mx. before the name.
You must also capitalize each name by calling the capitalizeName method.
I recommend calling capitalizeName before storing the name inside the array.
Finally, you will be required to write the two arrays to a file.
Each array should be written to a different file.
The 408 area code array should be written to a file called SJCustomers.txt
The 831 area code array should be written to a file called SCCustomers.txt
You must write the arrays to the file by calling the printArray method.
Note that you will need to call printArray twice - once for each array.
Method requirements
capitalizeName Method:
The method is named capitalizeName
It take in an array of Strings
It capitalizes all of the letters in each String of the array
This method must use a for loop. It cannot call any outside methods that we have not discussed in this class.
It returns nothing.
printArray Method:
The method is named printArray
It throws IOException
It takes in two parameters:
the first parameter is an array of Strings,
the second 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
It then closes the PrintWriter.
It returns nothing.
I got a starter code that I filled in, and here is my code so far, it is not working as it should so please adjust it and use only very basic intro java.
import java.io.*;
import java.util.Scanner;
public class AreaCodes {
public static void main(String[] args) throws IOException {
String name, gender, phone;
String SJ[] = new String[8];
String SC[] = new String[8];
File data = new File("customers.txt");
Scanner input = new Scanner(data);
int i = 0;
int j = 0;
while(input.hasNextLine()) {
name = input.nextLine();
gender = input.nextLine();
phone = input.nextLine();
if (gender.equalsIgnoreCase("m")) {
name = "Mr. " + name;
} else if (gender.equalsIgnoreCase("f")) {
name = "Ms. " + name;
} else if (gender.equalsIgnoreCase("o")) {
name = "Mx. " + name;
}
if (phone.substring(0, 3).equals("408")) {
SJ[i] = name;
} else {
SC[j] = name;
}
System.out.println(SJ[i]);
System.out.println(SC[j]);
i++;
j++;
}
capitalizeNames(SJ);
capitalizeNames(SC);
//printArray(area408, "customers.txt");
//printArray(area831, "customers.txt");
input.close();
}
public static void capitalizeNames(String[] names) {
return;
}
public static void printArray(String[] names, String fileName) throws IOException {
PrintWriter out = new PrintWriter("customers.txt");
for (int i = 0; i < names.length; i++) {
out.println(names[i]);
}
out.close();
return;
}
}
Below is the output that your program should give inside of the SJCustomers.txt file
MS. JIMING WU MS. LEANNA PEREZ MR. MOHAMMED ABBAS MS. KUMARI CHAKRABARTI MR. SHAKIL SMITH MR. PEDRO MARTINEZ MX. ALLY GU MR. ALVIN NGO
It should also give the below is the output in a second file called SCCustomers.txt file
MR. JAMES BROWN MR. XING LI MX. STACEY CAHILL MS. JUNG AHRIN MS. TAMARA WHITE MR. ABIR FADEL MR. BRAD FEINMAN MR. XIAOHANG YUE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started