Question: How to run the following code? It keeps to show Try running the program again. Provide 3 run-time parameters: 1) data source file path 2)
How to run the following code? It keeps to show "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." It needs to validate parameter first. How to validate parameter?
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=13486;
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
Get step-by-step solutions from verified subject matter experts
