MY CODE SO FAR: import java.util.Scanner; import java.io.*; public class Custome
ID: 3906784 • Letter: M
Question
MY CODE SO FAR:
import java.util.Scanner;
import java.io.*;
public class Customers
{
public static void capitalizeArray(String[] customers)
{
for (int i = 0; i < customers.length; i++) {
customers[i].toUpperCase();
}
return;
}
public static void printArray(String[] customers) throws IOException
{
PrintWriter out = new PrintWriter("customers.txt");
for (int i = 0; i < customers.length; i++) {
out.println(customers[i]);
}
return;
}
public static void main(String[] args) throws IOException
{
String customers[] = new String[15];
String name, gender;
File data = new File("names.txt");
Scanner input = new Scanner(data);
while (input.hasNextLine()) {
name = input.nextLine();
gender = input.nextLine();
};
capitalizeArray(customers);
printArray(customers);
}
}
THIS IS WHAT GOES IN TO names.txt
Barbara Stanton
F
Timoteo Martinez
M
Ally Gu
O
Xing Xiao
M
Dung Kim
F
Tanya White
F
Alan Ngo
M
Abir Fadel
M
Nataly Luna
F
Charles Atkinson
M
Stacey Cahill
O
Stephanie Teatro
F
Ping Li
M
June Park
F
Binh Nguyen
M
Assignment 21.2: Customer Data (10 pts) Write a program that reads in a series of names, along with their gender, from a file. The file is called names.txt. The program uses a loop to read in each name from the file and store each person's title (Mr. or Ms. or Mx.) name in an array called customers Note: you can assume that the size of the array is know ahead of time to be 15 The program should then call two methods. The first method: The method is named capitalizeArray It take in a String array It alters the values in the String array to capitalize all of the letters . This method must use a for loop. It cannot call any outside methods that we have not discussed in this class It returns nothing. The second method: The method is named print It takes in an array of Strings It opens up a new PrintWriter and connects it to a file called customers.txt It prints out the array to the file using a for loop It closes the PrintWriter It returns nothing Once you have filled the customers array with titles and names, call the capitalizeArray method to capitalize all the names in the array. Next, call the print method to display each of the capitalized names in the customers.txt file Copy and paste the starter code below into a new file called Customers.javaExplanation / Answer
/*
* Find the below code which modified as per your requirement. I have commented wherever required for understanding purpose
*/
import java.util.Scanner;
import java.io.*;
public class Customers
{
/*
* customers array variable must be class variable to store changes performing on it
* it has an modifier static reason main method is static which cannot access the non-static variable
*/
static String customers[] = new String[15];
//defined capitalizeArray function
public static void capitalizeArray(String[] customers)
{
for (int i = 0; i < customers.length; i++) {
//change customer title and name to uppercase and store it back as it is required "printArray" function
customers[i]=customers[i].toUpperCase();
}
return;
}
//defined printArray function
public static void printArray(String[] customers) throws IOException
{
//I have provided complete file path & file name to ouput Customer data. Here \ is used reason is is an escape character where it won't work correctly if you mention only one
PrintWriter out = new PrintWriter("C:\Users\Desktop\Java_Wokspace\Hello\src\customers.txt");
for (int i = 0; i < customers.length; i++) {
out.println(customers[i]); //prints customer data to customers.txt file
}
out.close(); //"PrintWriter" function must be closed otherwise it remains in buffered stage & output doesn't appears in the customers.txt file & it is mandatory
return;
}
public static void main(String[] args) throws IOException
{
String name, gender;
//given full path of input file with file name
File data = new File("C:\Users\Desktop\Java_Wokspace\Hello\src ames.txt");
Scanner input = new Scanner(data);
int i=0;//variable required to iterate an array
while (input.hasNextLine()) {//check next line exists in names.txt
name = input.nextLine();//read name of the customer
gender = input.nextLine();//read gender of the customer
if(gender.equalsIgnoreCase("M")) {//if gender is Male add title Mr. before name
customers[i]="Mr. "+name;//store customer with title & name
}else if(gender.equalsIgnoreCase("F")) {//if gender is Female add title Ms. before name
customers[i]="Ms. "+name;//store customer with title & name
}else if(gender.equalsIgnoreCase("O")) {//if gender is Male & Female add title Mx. before name
customers[i]="Mx. "+name;//store customer with title & name
}
i++; //increment i to store another customer
}
capitalizeArray(customers);//call function capitalizeArray
printArray(customers);//call function printArray
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.