Answer:
I am writing a JAVA program.
import java.util.Scanner; //class to take input from user
import java.io.File; //class for working with files
import java.io.FileNotFoundException; // indicates that file could not be opened
import java.io.PrintWriter; //used to print or formatted data
public class BabyNames{ //class name
public static void main(String[] args) throws FileNotFoundException{ //main function body throws FileNotFoundException to indicate if the file cannot be opened
File input_baby = new File("babynames.txt"); //creates object iFile and passes babynames.txt file name to it
File boy_output = new File("boynames.txt"); //creates object of File and passes boynames.txt name to it This file is to store boys names from babynames.txt
File girl_output = new File("girlnames.txt"); //creates object of File & passes girlnames.txt name to it. To store girls names from babynames
String[] boy_names = new String[100]; //create an array to hold boys names
String[] girl_names = new String[100]; //creates an array to hold girls names
PrintWriter boyNames = new PrintWriter(boy_output); //create object of PrintWriter for writing the separated boys names to boynames.txt file
PrintWriter girlNames = new PrintWriter(girl_output); //create object of PrintWriter for writing the separated girls names to girlnames.txt file
Scanner input = new Scanner(input_baby); //creates Scanner class object to scan babyname.txt
int i = 0;
while (input.hasNextLine()){ //checks if the babynames files has next line until the last line of the file
String str = input.nextLine(); //scans the current line
String[] strings = str.split(" "); //split the line into array of strings/words based on the space " " delimiter
boy_names[i] = strings[1]; //array to store boys names and strings[1] locates the boys names in input file
girl_names[i++] = strings[3]; } //array to store girls names strings[3] locates the girls names
for(i = 0; i < 100; i++){ //write the separated boys names to boynames.txt file and separated girls names in girlnames.txt file
boyNames.write(boy_names[i]);
boyNames.println();
girlNames.write(girl_names[i]);
girlNames.println();}
input.close();
boyNames.close();
girlNames.close(); } }
Step-by-step explanation:
The program has an input file babynames.txt and two text files to store separated boys and girls names. The while loop reads every line of the babynames.txt at each iteration and and splits each line into array of words strings based on the space delimiter. A line from babynames.txt file: 1 Michael 65,275 Jessica 46,470
boy_names[i] = strings[1] This line in loop basically locates boys names from each line of the babynames file. For the line given above. At first iteration of while loop when i=0 then boy_names[i] = strings[1] statement sets 1st index of strings array to the 0-th index of boy_names. Notice that 1st index of strings array is the second word of the above line i.e. Michael. So Michael is stored as 1st element of array boys_name.
girl_names[i++] = strings[3]; locates girls names in the same way as boy_names[i] = strings[1]
strings[3] means 3rd index of strings array Notice that 3rd index of strings array is Jessica.
There are total of 100 boys and 100 girls names in babynames.txt file so for(i = 0; i < 100; i++) this for loop prints these 100 separated boys and girls names in their respective files.
However this program only separates the boys and girls names and not their data such as in this line of input file Michael has data 65,275 and Jessica has 46,470. If you want to add this data into boynames.txt and girlnames.txt files then alter the above code. The altered code is attached.
create an array to store data of boys.
String[] boy_data = new String[100];
Create an array to store data of girls.
String[] girl_data = new String[100];
Now considering the same example 1 Michael 65,275 Jessica 46,470
boy_data[i] = strings[0] + " " + strings[1] + " " + strings[2]; the boy_data array stores data of boys i.e. the elements of 0th 1st and 2nd index. Notice that element at 0th index is the first element element i.e. 1, element at 1st index is Michael and element at 2nd index is 65275. So this is the data of Michael.
girl_data[i++] = strings[0] + " " + strings[3] + " " + strings[4]; } the girl_data array stores data of girls i.e. the elements of 0-th 3rd and 4th index. Notice that element at 0-th index is the first element element i.e. 1, element at 3rd index is Jessica and element at 4th index is 46,470. So this is the data of Jessica.
Now the outer loop iterates is used to print the separated data of boys and girls. 1st inner loop has an if statement if(boy_data[j].split(" ")[1].compareTo(boy_names[i]) == 0) which splits the boy_data and compareTo() method compares the first element of boy_data (Michael) to first element of the boy_names array (Michael). If both are same then it write the data i.e. to boynames.txt 1 Michael 65,275. Same works for the girls too. The program along with its output is attached.