0

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: enter image description here

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```
8
  • 1
    In the code it says you're using 1050 - Project 04 - Input.txt and what you posted is obviously not a text file. Commented Aug 3, 2021 at 0:23
  • 1
    @Dropout Whoops! I've been staring at all of this for so long that I posted the wrong screenshot lol. Fixed! The text file screen shot is now up. Commented Aug 3, 2021 at 0:30
  • 1
    Is it tab separated? If so, you might want to change the delimiter character of your Scanner, with the useDelimiter method. I would actually recommend using two Scanner objects - one that reads the file a line at a time, then one that reads a line of the file, one field at a time. Commented Aug 3, 2021 at 0:30
  • 1
    @Dropout @DawoodibnKareem Okay, here's my revised code. I made the delimiter \t (definitely helped!) But it's still giving me an input mismatch error, this time it's on line 84 qtrlyWages = inputFile.nextInt(); . It's funny, I noticed that if I comment out line 84 momentarily-- it starts to print 1) 1, Alabama -- Statewide, Federal Government, 0 and then it's back to the same mismatch error in line 73... which confuses me because (based on the small printout) qtr seems to be correct? Commented Aug 4, 2021 at 19:05
  • 1
    Hi catherinemallory, and kudos for sticking with this. I think what might be happening now is that because you're only using one scanner, and that scanner doesn't consider newline to be a delimiter, the fourth field on one line and the first field on the next line are being treated as a single field. You probably want a delimiter expression that allows both tabs and newlines to be delimiters. I think something like "\t|[\n\r]+" might work. Alternatively, you could use my idea of having two scanners - one to read each line from the file, and one to read each field from the line. Commented Aug 4, 2021 at 20:14

1 Answer 1

0

The issue is that your input file data actually starts at line 4, the first 3 lines are just file and column headers. Since you aren't skipping these lines an InputMismatchException occurs.

To skip the file header, use Scanner.nextLine() as

int headerLines = 3;

for (int i = 0; i < headerLines; i++) {
    inputFile.nextLine();
    numInputLines++; // if you want to include the header into the count
}

while (inputFile.hasNext()) {
   // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well that's one issue. Another problem is that inputFile.next() is only going to consume a single word, not the whole field; so when it's time to read the 79324 on the first line of data, you'll actually be reading the word Statewide.
@DawoodibnKareem Yes, you're correct. Personally, I would read the whole line and parse columns using a regex, but I don't want to spoon feed the complete solution. This post is just to get her past the exception, that's when she'll realize other problems with her code and see what a great advice using two scanners is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.