Question

In: Computer Science

ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...

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

Solutions

Expert Solution

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


Related Solutions

ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
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...
In this assignment you will write a program that encrypts a text file.  You will use the...
In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme. Ask the user for the name of the original file. Ask the user for the name of the output file. Ask the user for the encryption key, n. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array. Transpose the array.  (Exchange the rows and columns.) Write the characters from the array to an output...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to an output file.
Concepts tested by this program            Hash Table,            Link List,hash code, buckets/chaining,exception handling, read/write files (FileChooser)A concordance lists every word that occurs in a document in alphabetical order, and for each word it gives the line number of every line in the document where the word occurs.Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
Create a Matlab program to load in the attached file File_Q5_Use.csv. You will first need to...
Create a Matlab program to load in the attached file File_Q5_Use.csv. You will first need to click on the link to open it, then save it as a .csv file in the directory you are using in your Matlab programs before you can load it in to Matlab. It has 2 columns, the first column is the x values, the second column is the y values. Set Figure (1). Plot the points using red stars and a blue line with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT