In: Computer Science
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name.
CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
This program reads a file with numbers, and writes the numbers to
another
file, lined up in a column and followed by their total.
*/
public class Total
{
public static void main(String[] args) throws
FileNotFoundException
{
// Prompt for the input and output file names
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
System.out.print("Output file1: ");
String outputFileName1 = console.next();
System.out.print("Output file2: ");
String outputFileName2 = console.next();
// Construct the Scanner and PrintWriter objects for reading and writing
File inputFile = new File(inputFileName);
Scanner in = new Scanner("babynames.txt");
PrintWriter out1 = new PrintWriter("boysnames.txt");
PrintWriter out2 = new PrintWriter("girlsnames.txt");
// Read the input and write the output
double total = 0;
while (in.hasNextLine())
{
String A = in.next();
String B = in.next();
String C = in.next();
String D = in.next();
String E = in.next();
String F = in.next();
String G = in.next();
out.printf("%15.2f\n", value);
total = total + value;
}
out.printf("Total: %8.2f\n", total);
in.close();
out.close();
}
}
babynames.txt:
1 Michael 462085 2.2506 Jessica 302962 1.5436
2 Christopher 361250 1.7595 Ashley 301702 1.5372
3 Matthew 351477 1.7119 Emily 237133 1.2082
4 Joshua 328955 1.6022 Sarah 224000 1.1413
5 Jacob 298016 1.4515 Samantha 223913 1.1408
6 Nicholas 275222 1.3405 Amanda 190901 0.9726
7 Andrew 272600 1.3277 Brittany 190779 0.9720
8 Daniel 271734 1.3235 Elizabeth 172383 0.8783
9 Tyler 262218 1.2771 Taylor 168977 0.8609
10 Joseph 260365 1.2681 Megan 160312 0.8168
11 Brandon 259299 1.2629 Hannah 158647 0.8083
12 David 253193 1.2332 Kayla 155844 0.7940
13 James 244775 1.1922 Lauren 153530 0.7822
14 Ryan 241105 1.1743 Stephanie 149725 0.7628
15 John 239730 1.1676 Rachel 148907 0.7587
16 Zachary 225188 1.0968 Jennifer 147948 0.7538
17 Justin 220012 1.0716 Nicole 136033 0.6931
18 William 217588 1.0598 Alexis 131117 0.6680
19 Anthony 216088 1.0525 Victoria 117386 0.5981
20 Robert 205313 1.0000 Amber 115551 0.5887
Hello! :)
Here is the updated code to solve the assignment:
Babynames.java:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
This program reads a file with babynames, and creates two output
files, one file listing out all boys names, and the other file
listing out all girls names.
*/
public class Babynames
{
public static void main(String[] args) throws FileNotFoundException
{
// Prompt for the input and output file names
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
System.out.print("Output file1: ");
String outputFileName1 = console.next();
System.out.print("Output file2: ");
String outputFileName2 = console.next();
// Construct the Scanner and PrintWriter objects for reading and writing
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
File outputFile1 = new File(outputFileName1);
PrintWriter out1 = new PrintWriter(outputFile1);
File outputFile2 = new File(outputFileName2);
PrintWriter out2 = new PrintWriter(outputFile2);
// Read the input and write the output
while (in.hasNext())
{
String A = in.next();
String B = in.next();
String C = in.next();
String D = in.next();
String E = in.next();
String F = in.next();
String G = in.next();
out1.println(B);
out2.println(E);
}
in.close();
out1.close();
out2.close();
}
}
Here is a log of a demo run:
❯ javac Babynames.java
❯ java Babynames
Input file: babynames.txt
Output file1: boysnames.txt
Output file2: girlsnames.txt
❯ cat babynames.txt
1 Michael 462085 2.2506 Jessica 302962 1.5436
2 Christopher 361250 1.7595 Ashley 301702 1.5372
3 Matthew 351477 1.7119 Emily 237133 1.2082
4 Joshua 328955 1.6022 Sarah 224000 1.1413
5 Jacob 298016 1.4515 Samantha 223913 1.1408
6 Nicholas 275222 1.3405 Amanda 190901 0.9726
7 Andrew 272600 1.3277 Brittany 190779 0.9720
8 Daniel 271734 1.3235 Elizabeth 172383 0.8783
9 Tyler 262218 1.2771 Taylor 168977 0.8609
10 Joseph 260365 1.2681 Megan 160312 0.8168
11 Brandon 259299 1.2629 Hannah 158647 0.8083
12 David 253193 1.2332 Kayla 155844 0.7940
13 James 244775 1.1922 Lauren 153530 0.7822
14 Ryan 241105 1.1743 Stephanie 149725 0.7628
15 John 239730 1.1676 Rachel 148907 0.7587
16 Zachary 225188 1.0968 Jennifer 147948 0.7538
17 Justin 220012 1.0716 Nicole 136033 0.6931
18 William 217588 1.0598 Alexis 131117 0.6680
19 Anthony 216088 1.0525 Victoria 117386 0.5981
20 Robert 205313 1.0000 Amber 115551 0.5887
❯ cat boysnames.txt
Michael
Christopher
Matthew
Joshua
Jacob
Nicholas
Andrew
Daniel
Tyler
Joseph
Brandon
David
James
Ryan
John
Zachary
Justin
William
Anthony
Robert
❯ cat girlsnames.txt
Jessica
Ashley
Emily
Sarah
Samantha
Amanda
Brittany
Elizabeth
Taylor
Megan
Hannah
Kayla
Lauren
Stephanie
Rachel
Jennifer
Nicole
Alexis
Victoria
Amber
Hope this helps! :)