How can I run below 2 java file and output the .txt file on the java import java
ID: 3705126 • Letter: H
Question
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;
}
}
}
Explanation / Answer
Store both the files as
SmallAreaIncomePovertyReport.java
SmallAreaIncomePovertyDataEditor.java
There should also be an input file to the program "FormattedReportData.dat" file
Run the following commands to Compile and execute
1) java SmallAreaIncomePovertyReport.java
2) java SmallAreaIncomePovertyDataEditor.java
3) javac SmallAreaIncomePovertyReport.java FormattedReportData.dat 12
12 can be anything. It represents number of records. But it should be an integer
The program will take 2 command line parameters
a) File name
b) Number of records
Since I dont have FormattedReportData.dat . I cant run the code. I have shared the instaruction to run. Run and let me know if you face any issue. I will help.
Comment if you have any issues/concern.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.