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
If you have any problem with the code feel free to comment.
Program
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 Test {
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
//setting up files
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out1 = new PrintWriter(outputFileName1);
PrintWriter out2 = new PrintWriter(outputFileName2);
//clearing the file if it contains any data
out1.write("");
out2.write("");
// Read the input and write the output
//reading file
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();
//writing to boys file
out1.append(A+" "+B+" "+C+" "+D+"\n");
//writing to girls file
out2.append(A+" "+E+" "+F+" "+G+"\n");
}
//closing the files
in.close();
out1.close();
out2.close();
console.close();
}
}
Input
Output