I'm still learning java so excuse my lack of skill here, any help is appreciated! I've been working on this java I/O project for school all day at this point and now I'm getting this input mismatch error on line 73 qtr = inputFile.nextInt();
. The value should be an int, so I'm not sure if maybe I'm pointing java in the wrong direction or something instead?
Here is my code
import java.util.Scanner; // Access the Scanner class
import java.io.*; // Access PrintWriter and related classes
public class FileInputOutputDemo {
public static void main(String[] args) throws IOException {
// Declare variables
// Define the file names for this demo program
final String INPUT_FILE = ("C:\\Users\\Hilary\\Desktop\\CS 1050 Summer 2021\\Project 4\\1050 - Project 04 - Input.txt");
final String OUTPUT_FILE = "Proj4OutputDemo_Output.txt";
int numInputLines = 0; // Number of lines in the input file
int qtr = 0; // Quarter
String areaTitle = ""; // Area title
String ownerTitle = ""; // Owner title
double qtrlyWages = 0.0; // Quarterly wages
int errorCounter = 0;
int totalDollarQ1FED[];
int totalDollarQ1STATE[];
int totalDollarQ1LOC[];
int totalDollarQ1PRIV[];
int totalDollarQ2FED[];
int totalDollarQ2STATE[];
int totalDollarQ2LOC[];
int totalDollarQ2PRIV[];
String line = "";
// Access the input/output files
File inputDataFile = new File(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
// Begin program execution
line = "Reading file " + INPUT_FILE + "\n" +
"Creating file " + OUTPUT_FILE + "\n";
writeInfo(outputFile, line, 3);
// Read and echo the input file
while (inputFile.hasNext()) {
numInputLines++;
qtr = inputFile.nextInt();
areaTitle = inputFile.next();
ownerTitle = inputFile.next();
qtrlyWages = inputFile.nextDouble();
line = numInputLines + ") " + qtr + ", " + areaTitle +
", " + ownerTitle + ", " + qtrlyWages;
writeInfo(outputFile, line,numInputLines);
} // End while
line = "\n# of input lines: " + numInputLines;
writeInfo(outputFile, line, 0);
// Without the close, no output is written to the output file!
outputFile.close();
inputFile.close();
}
Here is a screenshot snippet of the beginning of my input file:
Thank you!!!
REVISED CODE 8/4
import java.io.*; // Access PrintWriter and related classes
public class FileInputOutputDemo {
public static void main(String[] args) throws IOException {
// Declare variables
// Define the file names for this demo program
final String INPUT_FILE = ("C:\\Users\\Hilary\\Desktop\\CS 1050 Summer 2021\\Project 4\\1050 - Project 04 - Input.txt");
final String OUTPUT_FILE = "Proj4OutputDemo_Output.txt";
int numInputLines = 0; // Number of lines in the input file
int qtr = 0; // Quarter
String areaTitle = ""; // Area title
String ownerTitle = ""; // Owner title
int qtrlyWages = 0; // Quarterly wages
int errorCounter = 0;
int totalDollarQ1FED[];
int totalDollarQ1STATE[];
int totalDollarQ1LOC[];
int totalDollarQ1PRIV[];
int totalDollarQ2FED[];
int totalDollarQ2STATE[];
int totalDollarQ2LOC[];
int totalDollarQ2PRIV[];
String line = "";
int headerLines = 3;
// Access the input/output files
File inputDataFile = new File(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
// Scanner tabbed = new Scanner(inputDataFile);
inputFile.useDelimiter("\t");
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
// Begin program execution
line = "Reading file " + INPUT_FILE + "\n" +
"Creating file " + OUTPUT_FILE + "\n";
writeInfo(outputFile, line, 3);
// Read and echo the input file
for (int i = 0; i < headerLines; i++) {
inputFile.nextLine();
// numInputLines++;
}
while (inputFile.hasNext()) {
numInputLines++;
qtr = inputFile.nextInt();
areaTitle = inputFile.next();
ownerTitle = inputFile.next();
qtrlyWages = inputFile.nextInt();
line = numInputLines + ") " + qtr + ", " + areaTitle +
", " + ownerTitle + ", " + qtrlyWages;
writeInfo(outputFile, line,numInputLines);
} // End while
line = "\n# of input lines: " + numInputLines;
writeInfo(outputFile, line, 0);
// Without the close, no output is written to the output file!
outputFile.close();
inputFile.close();
} // End main
// ***********************************************************************
/**
writeInfo - Write info to the console and to an output file
@param out - output file reference variable
@param dataLine - a string with the line to output
@param - numLines - if > 0, the output line # counter
@return - none
Note: if numLines > 0, this method inserts a blank line after
SPACING lines
*/
public static void writeInfo(PrintWriter out, String dataLine, int numLines)
{
final int SPACING = 5; // Number of lines to introduce a blank line
out.println(dataLine);
System.out.println(dataLine);
if (numLines % SPACING == 0 && numLines > 0)
{
out.println();
System.out.println();
}
} // End writeInfo
} // End class```