Question

Write a program that reads in BabyNames.txt and produces two files, boynames.txt and girlnames.txt, separating the...

Write a program that reads in BabyNames.txt and produces two files, boynames.txt and girlnames.txt, separating the data for the boys and the girls, and listing them in alphabetical order.

This is the code I have, Its not sorting the boys/girls names in seperate files. It is creatinf two files, but has all the names. I also need to alphabetize the names. with an Array. sort

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class BabyNames{
public static void main(String[] args) throws FileNotFoundException{
File baby = new File("BabyNames.txt");
File boy = new File("boynames.txt");
File girl = new File("girlnames.txt");
PrintWriter printBoy = new PrintWriter(boy);
PrintWriter printGirl = new PrintWriter(girl);
{
Scanner sc = new Scanner(baby);
while (sc.hasNextLine()){
String s = sc.nextLine();
String[] names = s.split(" ");
for(int i=0;i if(i==0){
printBoy.write(names[i] + " ");
printGirl.write(names[i] + " ");
}
else
{
if(i==1 || i==2)
printBoy.write(names[i] + " ");
else
printGirl.write(names[i] + " ");
}
}
printBoy.println();
printGirl.println();
}
sc.close();
printBoy.close();
printGirl.close();
}
}
}

1 Michael 65,275 Jessica 46,470
2 Christopher 52,331 Ashley 45,553
3 Matthew 44,794 Brittany 36,534
4 Joshua 43,216 Amanda 34,405
5 Daniel 33,809 Samantha 25,865
6 David 33,739 Sarah 25,810
7 Andrew 33,653 Stephanie 24,859
8 James 32,344 Jennifer 22,219
9 Justin 30,635 Elizabeth 20,742
10 Joseph 30,124 Lauren 20,499
11 Ryan 29,242 Megan 20,256
12 John 29,066 Emily 19,361
13 Robert 28,865 Nicole 17,950
14 Nicholas 27,899 Kayla 17,536
15 Anthony 25,074 Amber 15,862
16 William 24,887 Rachel 15,704
17 Jonathan 24,064 Courtney 15,378
18 Kyle 22,698 Danielle 14,330
19 Brandon 22,155 Heather 14,217
20 Jacob 22,005 Melissa 13,997
21 Tyler 20,631 Rebecca 13,670
22 Zachary 20,424 Michelle 13,418
23 Kevin 20,403 Tiffany 13,157
24 Eric 19,412 Chelsea 12,782
25 Steven 18,753 Christina 11,925
26 Thomas 18,214 Katherine 11,618
27 Brian 16,919 Alyssa 11,262
28 Alexander 16,183 Jasmine 11,033
29 Jordan 16,131 Laura 10,908
30 Timothy 15,685 Hannah 10,274
31 Cody 15,392 Kimberly 10,193
32 Adam 14,708 Kelsey 9,494
33 Benjamin 14,619 Victoria 9,104
34 Aaron 14,550 Sara 8,973
35 Richard 13,574 Mary 8,665
36 Patrick 12,373 Erica 8,604
37 Sean 12,014 Alexandra 8,507
38 Charles 11,991 Amy 8,457
39 Stephen 11,940 Crystal 8,218
40 Jeremy 11,685 Andrea 8,211
41 Jose 11,508 Kelly 8,134
42 Travis 11,454 Kristen 8,073
43 Jeffrey 10,979 Erin 7,987
44 Nathan 10,934 Brittney 7,427
45 Samuel 10,883 Anna 7,290
46 Jason 10,680 Taylor 7,255
47 Mark 10,680 Maria 7,181
48 Jesse 8,980 Allison 7,131
49 Paul 8,579 Cassandra 7,052
50 Dustin 8,455 Caitlin 7,026
51 Gregory 8,377 Lindsey 6,781
52 Kenneth 8,258 Angela 6,664
53 Scott 8,083 Katie 6,513
54 Derek 8,075 Alicia 6,510
55 Austin 7,909 Jamie 6,509
56 Corey 7,690 Vanessa 6,405
57 Bryan 7,436 Kathryn 6,350
58 Ethan 6,988 Morgan 6,047
59 Alex 6,941 Jordan 5,955
60 Christian 6,776 Whitney 5,904
61 Juan 6,737 Brianna 5,678
62 Cameron 6,663 Christine 5,676
63 Jared 6,639 Natalie 5,633
64 Taylor 6,574 Lisa 5,344
65 Bradley 6,431 Kristin 5,300
66 Luis 5,788 Alexis 5,256
67 Cory 5,763 Jacqueline 5,237
68 Edward 5,741 Shannon 5,185
69 Shawn 5,719 Lindsay 5,037
70 Ian 5,460 Brooke 4,988
71 Evan 5,307 Catherine 4,893
72 Marcus 5,296 Olivia 4,623
73 Shane 5,273 April 4,570
74 Peter 5,215 Erika 4,566
75 Carlos 5,207 Katelyn 4,486
76 Trevor 5,140 Monica 4,452
77 Antonio 4,864 Kristina 4,389
78 Vincent 4,798 Kaitlyn 4,313
79 George 4,564 Paige 4,112
80 Keith 4,481 Molly 3,964
81 Phillip 4,453 Jenna 3,957
82 Victor 4,441 Leah 3,924
83 Dylan 4,414 Julia 3,909
84 Brett 4,392 Bianca 3,827
85 Chad 4,349 Tara 3,821
86 Nathaniel 4,342 Melanie 3,812
87 Donald 4,177 Marissa 3,806
88 Caleb 4,153 Cynthia 3,794
89 Casey 4,130 Holly 3,771
90 Jesus 4,127 Abigail 3,718
91 Blake 4,101 Meghan 3,692
92 Raymond 4,061 Kathleen 3,669
93 Mitchell 4,019 Julie 3,655
94 Adrian 3,999 Ariel 3,605
95 Joel 3,998 Alexandria 3,594
96 Erik 3,950 Veronica 3,587
97 Ronald 3,900 Patricia 3,578
98 Devin 3,862 Diana 3,544
99 Garrett 3,756 Gabrielle 3,541
100 Gabriel 3,694 Shelby 3,517

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

I've commented out some of your code that I didn't need. My logic is, I've first copied the names into names array and details array separately for both boys and girls. Then I've sorted both the arrays based on the names array.

Code:

import java.io.File;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class BabyNames{
public static void main(String[] args) throws FileNotFoundException{
File baby = new File("BabyNames.txt");
File boy = new File("boynames.txt");
File girl = new File("girlnames.txt");
String[] boys = new String[100];
String[] boydetails = new String[100];
String[] girls = new String[100];
String[] girldetails = new String[100];
PrintWriter printBoy = new PrintWriter(boy);
PrintWriter printGirl = new PrintWriter(girl);
Scanner sc = new Scanner(baby);
//Reading each line and splitting them on whitespaces
//Then storing the names and their full details in two separate arrays
//For both boys and girls
int i = 0;
while (sc.hasNextLine()){
String s = sc.nextLine();
String[] names = s.split(" ");
boys[i] = names[1]; //Storing boy names
boydetails[i] = names[0] + " " + names[1] + " " + names[2]; //Storing boy details
girls[i] = names[3]; //Storing girl names
girldetails[i++] = names[0] + " " + names[3] + " " + names[4]; //Storing girl details
}
//Sorting
Arrays.sort(boys);
Arrays.sort(girls);

//Writing into files
for(i = 0; i < 100; i++)
{
    //Writing boy names
    for(int j = 0; j < 100; j++){
        if(boydetails[j].split(" ")[1].compareTo(boys[i]) == 0)
        {
            printBoy.write(boydetails[j]);
            printBoy.println();
            break;
        }
    }
  
    //Writing girl names
    for(int j = 0; j < 100; j++){
        if(girldetails[j].split(" ")[1].compareTo(girls[i]) == 0)
        {
            printGirl.write(girldetails[j]);
            printGirl.println();
        }
    }
}
//Commenting out some of your code that is now not needed.
/*printBoy.write(names[i] + " ");
//printGirl.write(names[i] + " ");
}
else
{
if(i==1 || i==2)
printBoy.write(names[i] + " ");
else
printGirl.write(names[i] + " ");
}
}
printBoy.println();
printGirl.println();
}
*/
sc.close();
printBoy.close();
printGirl.close();
}
}

Output:

boynames-Notepac File Edit Format View Help 34 Aaron 14,550 32 Adam 14,708 94 Adrian 3.999 59 Alex 6,941 28 Alexander 16,183

Add a comment
Know the answer?
Add Answer to:
Write a program that reads in BabyNames.txt and produces two files, boynames.txt and girlnames.txt, separating the...
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
  • The text files boynames.txt and girlnames.txt, which are included in the source code for this boo...

    The text files boynames.txt and girlnames.txt, which are included in the source code for this book, contain lists of the 1,000 most popular boy and girl names in the United States for the year 2005, as compiled by the Social Security Administration. These are blank-delimited files where the most popular name is listed first, the second most popular name is listed second, and so on to the 1,000th most popular name, which is listed last. Each line consists of the...

  • I need help with this question for programming. The language we use is C++ The program...

    I need help with this question for programming. The language we use is C++ The program should ask the user if they want to search for a boy or girl name. It should then ask for the name search the appropriate array for the name and display a message saying what ranking that name received, if any. The name files (BoyNames.txt and GirlNames.txt) are in order from most popular to least popular and each file contains two hundred names. The...

  • i have to write a program that asks the names of 2 files. the first should...

    i have to write a program that asks the names of 2 files. the first should be open for reading second for writing. the program should read the the contents of the first file change all characters to uppercase and store in the second file. the second file will be a copy of the first file, except that all characters will be uppercase. use notepad to test the program. i got this so far {         String firstfile;         String...

  • **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...

    **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by white space. Write a program named Add2 that writes the sum of the first integers of these two files to a file named outdata1. It writes the sum of the first integers of these two files to a file named outdata2. Each sum is written on a line by itself. So, if the contents of indata1 were "37 6 90" and the contents of...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Write a program to create a file named integerFile.txt if it does not exist. Write 100...

    Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • Need help with the code for this assignment. Python Assignment: List and Tuples We will do...

    Need help with the code for this assignment. Python Assignment: List and Tuples We will do some basic data analysis on information stored in external files. GirNames.txt contains a list of the 200 most popular names given to girls born in US from year 2000 thru 2009: (copy link and create a txt file): https://docs.google.com/document/d/11YCVqVTrzqQgp2xJqyqPruGtlyxs2X3DFWn1YUb3ddw/edit?usp=sharing BoyNames.txt contains a list of the 200 most popular names given to boys born in US from year 2000 thru 2009 (copy link and create...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

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
ADVERTISEMENT