Question & Answer: How to run the following code? It keeps to show "Try running the program again. Provide 3 run-time parameters: 1) data source f…..

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.*;

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: How to run the following code? It keeps to show "Try running the program again. Provide 3 run-time parameters: 1) data source f…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

public class SmallAreaIncomePovertyDataEditor

{

public static void main(String[] args)

{

// checks the number of run-time parameters

if (args.length != 3)

{

System.out.println(“nTry 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.n”);

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(“nTry running the program again. Make sure”

+ ” the first run-time parameter contains the correct file ”

+ ” path to SmallAreaIncomePovertyEstData.txt.n”);

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(“nTry running the program again. Make sure”

+ ” the second run-time parameter contains a correct file ”

+ ” path for the FormattedReportData.dat file.n”);

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(“nTry running the program again. The third”

+ ” run-time parameter needs to be an integer.n”);

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 + “n”);

}

 

// prints a message confirming successful completion of operations

System.out.println(“nThe data has been edited and saved to ”

+ args[1] + ” successfully.n”);

 

}

catch (IOException e)

{ // checks for IOExceptions

System.out.println(“/nI/O Exception: ” + e.getMessage() + “n”);

return;

}

}

}

Expert Answer

 

The following is correct only for taking inputs for runtime parameters you need to use different techniques in eclipse

or any IDE

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(“nTry 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.n”);
return;
}

// checks the correctness of the first run-time parameter
File fIn = new File(args[1]);
if (!fIn.exists() ||
!args[1].endsWith(“SmallAreaIncomePovertyEstData.txt”))
{
System.out.println(“nTry running the program again. Make sure”
+ ” the first run-time parameter contains the correct file ”
+ ” path to SmallAreaIncomePovertyEstData.txt.n”);
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(“nTry running the program again. Make sure”
+ ” the second run-time parameter contains a correct file ”
+ ” path for the FormattedReportData.dat file.n”);
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(“nTry running the program again. The third”
+ ” run-time parameter needs to be an integer.n”);
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 + “n”);
}

// prints a message confirming successful completion of operations
System.out.println(“nThe data has been edited and saved to ”
+ args[1] + ” successfully.n”);

}
catch (IOException e)
{ // checks for IOExceptions
System.out.println(“/nI/O Exception: ” + e.getMessage() + “n”);
return;
}
}
}

For validating the run time parameter please follow the following steps

1. Run the program by run as java application

2. Then go to RUN -> Run Configurations

Question & Answer: How to run the following code? It keeps to show "Try running the program again. Provide 3 run-time parameters: 1) data source f..... 13. Click on the Arguments tab and Pass the Arguments in Program Arguments tab one by one following by a space

Question & Answer: How to run the following code? It keeps to show "Try running the program again. Provide 3 run-time parameters: 1) data source f..... 24. Then Click on the Run button, Hope your program will give desired output. Like this

Question & Answer: How to run the following code? It keeps to show "Try running the program again. Provide 3 run-time parameters: 1) data source f..... 3

Still stressed from student homework?
Get quality assistance from academic writers!