Can I get help on this in java please Update the program so
Can I get help on this in java please?
Update the program so that it displays everything you already have, plus two more summary lines. The summary lines show how many even numbers were processed and how many odd numbers.
The output should now look like this:
Row 1 sum: 30
Row 2 sum: 48
Row 3 sum: 55
Count: 12
Grand total: 133
Mean: 11.08
Even numbers: 7
Odd numbers: 5
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Part 2
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Your main() method is now too big and bulky. Put it on a diet. Copy the source code from previouse lab and paste it into a new file.
Have main() prompt the user for the filename and store it to an input-capture variable.
Create a new value-returning method named fiProcessFile.
The method must receive one parameter, the fully qualified filename.
The method must return 1 if the file was able to be processed (i.e., opened and read) or 0 if the file could not be (file not found).
Move all your file handling, calculations, and display into the new method, thus slimming down main() considerably. Some variable declarations and some instantiations will need to move, too, along with steps to initialize and manage counter and accumulators.
Use main() to display the success or failure of the file processing operation:
File processing complete
or
Unable to process file; file not found
Finally, modify your source code so that your output is ALSO printed out to a file named 9_Out.txt; output to the same folder location as your input.
Verify that the file exists and contains the proper output by searching for it and opening it in Notepad.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
code so far:
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
public class Lab {
public static void main(String [] args)
{
int iVal;
int iRow;
int iRowSum;
int iToken;
int totalSum = 0;
int totalAll = 0;
iRowSum = 0;
iRow = 1;
try
{
Scanner ifsInput = new Scanner(new File(\"C:\\\\Users\\\\path\\\\9_1_Input.txt\"));
while(ifsInput.hasNextLine())
{
while(ifsInput.hasNextInt())
{
for(iToken = 0; iToken <= 3; iToken++)
{
iVal = ifsInput.nextInt();
iRowSum = iRowSum + iVal;
totalAll++;
}
// adding into totalSum
totalSum = totalSum + iRowSum;
System.out.println(\"Row \" + iRow + \" sum: \" + iRowSum);
iRowSum = 0;
iRow++;
}
}
System.out.println(\"Count: \"+totalAll);
System.out.println(\"Grand total: \"+totalSum);
System.out.println(\"Mean: \"+((double)totalSum/totalAll));
}
catch (FileNotFoundException sMsg)
{
System.out.println(\"The file cannot be found or opened.\");
}
}
}
Solution
//input file name like C:\\Users\\path\\9_1_Input.txt
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;
public class Lab1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(\"Enter the file name to be processed.\");
System.out.flush();
String filename = scanner.nextLine();
int isFile = fiProcessFile(filename);
if (isFile == 1) {
System.out.println(\"File processing complete\");
} else {
System.out.println(\"Unable to process file; file not found\");
}
scanner.close();
}
public static int fiProcessFile(String filename) {
File f = new File(filename);
if (f.exists() && !f.isDirectory()) {
int iVal;
int iRow = 1;
int iRowSum = 0;
int iToken;
int evenCount = 0;
int oddCount = 0;
int totalSum = 0;
int totalAll = 0;
try (Scanner ifsInput = new Scanner(new File(filename));
Writer writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filename.substring(0, filename.lastIndexOf(\"\\\\\")) + \"\\\\9_Out.txt\"), \"utf-8\"))) {
while (ifsInput.hasNextLine()) {
ifsInput.nextLine();
while (ifsInput.hasNextInt()) {
for (iToken = 0; iToken <= 3; iToken++) {
iVal = ifsInput.nextInt();
iRowSum = iRowSum + iVal;
if (iVal % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
totalAll++;
}
totalSum = totalSum + iRowSum;
System.out.println(\"Row \" + iRow + \" sum: \" + iRowSum);
writer.write(\"Row \" + iRow + \" sum: \" + iRowSum + \"\ \");
iRowSum = 0;
iRow++;
}
}
System.out.println(\"File could not be created.\");
System.out.println(\"Count: \"+totalAll);
System.out.println(\"Grand total: \"+totalSum);
System.out.println(\"Mean: \"+((double)totalSum/totalAll));
System.out.println(\"Even numbers: \" + evenCount );
System.out.println(\"Odd numbers: \" + oddCount );
writer.write(\"Count: \" + totalAll + \"\ \");
writer.write(\"Grand total: \" + totalSum + \"\ \");
writer.write(\"Mean: \" + ((double) totalSum / totalAll) + \"\ \");
writer.write(\"Even numbers \" + evenCount + \"\ \");
writer.write(\"Odd numbers \" + oddCount + \"\ \");
return 1;
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
return 0;
} catch (IOException fileIOException) {
System.out.println(\"File could not be created.\");
fileIOException.printStackTrace();
return 0;
}
} else
return 0;
}
}



