Please help, I cannot for the life of me figure out why my output file is created, but there is nothing written to it. I read on a similar question that I had to close the output file as the information is buffered and only outputs when the file is closed. I had already done that before reading that post, other than that I cannot find any other post that helps me with this
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Arrays;
public class MyFile {
public static void main(String[] args) {
try {
// creating a File instance to reference text file in Java
File inputFile = new File("C:\\input.txt");
//creating a Scanner instance to read File in Java
Scanner scanfile = new Scanner(inputFile);
//creating a file to output results of program to
Formatter outputfile = new Formatter("C:\\output.txt");
//read each line of file using the Scanner instance "scanfile"
while (scanfile.hasNextLine()) {
String line = scanfile.nextLine();
//splitting String "line" into 2 parts at the ":"
String[] parts = line.split(":");
String part1 = parts[0];
String part2 = parts[1];
//splitting part2 into an array of integers at ","
String[] lineNumString = part2.split(",");
int[] lineNums = new int[lineNumString.length];
for (int i = 0; i < lineNumString.length; i++ ) {
lineNums[i] = Integer.parseInt(lineNumString[i]);
}
/*now that we have the line split into an operator (min, max or avg) and an array of numbers
we can identify the operator with an if and else if statements, then manipulate the integer array according
to the operator*/
//Outputting max value if operator is max
if (part1 == "max") {
String outputText = "The Max of " + Arrays.toString(lineNums) + " is " + maxOfArray(lineNums) + ". \r\n";
outputfile.format("%s", outputText);
}
//Outputting min value if operator is min
else if (part1 == "min") {
String outputText = "The Min of " + Arrays.toString(lineNums) + " is " + minOfArray(lineNums) + ". \r\n";
outputfile.format("%s", outputText);
}
//Outputting avg value if operator is avg
else if (part1 == "avg") {
String outputText = "The Min of " + Arrays.toString(lineNums) + " is " + avgOfArray(lineNums) + ". \r\n";
outputfile.format("%s", outputText);
}
}
scanfile.close();
outputfile.close();
}
// print out error messages for input file not found or for being unable to create or write to an output file.
catch (FileNotFoundException e) {
System.out.println("Error.");
}
}
// method for determining the max value in an array of integers
public static int maxOfArray(int[] inputArray) {
int maxValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] > maxValue) {
maxValue = inputArray[i];
}
}
return maxValue;
}
// method for determining the min value in an array of integers
public static int minOfArray(int[] inputArray) {
int minValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] < minValue) {
minValue = inputArray[i];
}
}
return minValue;
}
//method for determining avg value of an array of integers
public static int avgOfArray(int[] inputArray) {
int sum = 0;
for (int i : inputArray) {
sum += i;
}
int average = sum / inputArray.length;
return average;
}
}