Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I run below 2 java file and output the .txt file on the java import java.io.*; public class SmallAreaIncomePovertyReport { // creates a

How can I run below 2 java file and output the .txt file on the java

import java.io.*; public class SmallAreaIncomePovertyReport { // creates a Console instance used to print formatted text static Console con = System.console(); // initializes variables used for statistical calculations by state static String stCode = null; static int stPop = 0; static int stChildPop = 0; static int stChildPovPop = 0; static float stPerChildPov = 0f; public static void main(String[] args) { // checks the number of run-time parameters if (args.length != 2) { System.out.println(" Try running the program again. Provide 2" + " run-time parameters: 1) input file path 2) number of" + " records."); return; } // checks the correctness of the first run-time parameter File fIn = new File(args[0]); if (!fIn.exists() || !args[0].endsWith("FormattedReportData.dat")) { System.out.println(" Try running the program again. Make sure" + " the first run-time parameter contains the correct file " + " path to FormattedReportData.dat. "); return; } // checks the correctness of the second run-time parameter int numOfRecs; try { numOfRecs = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.out.println(" Try running the program again. The second" + " run-time parameter needs to be an integer. "); return; } // prints out the headers System.out.println(" File: " + args[0] + " "); String headFormat = "%5s %11s %17s %25s %16s %n"; con.printf(headFormat, "State", "Population", "Child Population", "Child Poverty Population", "% Child Poverty"); con.printf(headFormat, "-----", "----------", "----------------", "------------------------", "---------------"); // initializes the buffered input device try (BufferedReader br = new BufferedReader(new FileReader(fIn))) { // initializes variables used to store data from a new line String newLineStCode; int newLinePop; int newLineChildPop; int newLineChildPovPop; for (int line = 1; line <= numOfRecs; line++) { // executes the input process line by line // reads in and parses data from a new line String[] newLine = br.readLine().split(","); newLineStCode = newLine[0]; newLinePop = Integer.valueOf(newLine[1]); newLineChildPop = Integer.valueOf(newLine[2]); newLineChildPovPop = Integer.valueOf(newLine[3]); if (stCode != null && !newLineStCode.equals(stCode)) { // data for a state different from before was read-in // prints a line the report printReportLine(); // sets the counters the counters stPop = 0; stChildPop = 0; stChildPovPop = 0; } // uses data from the new line to update the state variables stCode = newLineStCode; stPop += newLinePop; stChildPop += newLineChildPop; stChildPovPop += newLineChildPovPop; } // prints the final line of the report printReportLine(); System.out.println(""); } catch (IOException e) { System.out.println("/nI/O Exception: " + e.getMessage() + " "); } } // prints a line of the report in the correct format public static void printReportLine() { stPerChildPov = (((float)stChildPovPop/(float)stChildPop)*100); con.printf("%5s %,11d %,17d %,25d %16.2f %n", stCode, stPop, stChildPop, stChildPovPop, stPerChildPov); } }import java.io.*; public class SmallAreaIncomePovertyDataEditor { public static void main(String[] args) { // checks the number of run-time parameters if (args.length != 3) { System.out.println(" Try running the program again. Provide 3" + " run-time parameters: 1) data source file path 2)" + " destination file path 3) number of records in the data" + " file. "); return; } // checks the correctness of the first run-time parameter File fIn = new File(args[0]); if (!fIn.exists() || !args[0].endsWith("SmallAreaIncomePovertyEstData.txt")) { System.out.println(" Try running the program again. Make sure" + " the first run-time parameter contains the correct file " + " path to SmallAreaIncomePovertyEstData.txt. "); return; } // checks the correctness of the second run-time parameter File fOut = new File(args[1]); if (!fOut.getParentFile().exists() || !args[1].endsWith("FormattedReportData.dat")) { System.out.println(" Try running the program again. Make sure" + " the second run-time parameter contains a correct file " + " path for the FormattedReportData.dat file. "); return; } // checks the correctness of the third run-time parameter int numOfRecs; try { numOfRecs = Integer.parseInt(args[2]); } catch (NumberFormatException e) { System.out.println(" Try running the program again. The third" + " run-time parameter needs to be an integer. "); return; } // initializes the buffered I/O devices try (BufferedReader br = new BufferedReader(new FileReader(fIn)); BufferedWriter bw = new BufferedWriter(new FileWriter(fOut))) { for (int line = 1; line <= numOfRecs; line++) { // executes the I/O process line by line // reads in the current line String currLine = br.readLine(); // finds and processes relevant data String stateCode = currLine.substring(0, 2); String pop = currLine.substring(82, 90).trim(); String childPop = currLine.substring(91, 99).trim(); String childPovPop = currLine.substring(100, 108).trim(); // saves processed data as one line of the output file bw.write(stateCode + "," + pop + "," + childPop + "," + childPovPop + " "); } // prints a message confirming successful completion of operations System.out.println(" The data has been edited and saved to " + args[1] + " successfully. "); } catch (IOException e) { // checks for IOExceptions System.out.println("/nI/O Exception: " + e.getMessage() + " "); return; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions