1

Can anyone please tell me why I keep getting InputMismatchExceptions? I have been stuck trying to understand whats happening and just can't seem to figure it out. The program is suppose to read a input file of high temperatures and then create a histogram of the temperatures read.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class CoolWeather
{
    static JFileChooser selecter;
    static Scanner in;
    public static void main(String[] args) throws FileNotFoundException
    {
       File inputFile;
       selecter = new JFileChooser(".");
       int status = selecter.showOpenDialog(null);
       if(status != JFileChooser.APPROVE_OPTION) 
       {
            JOptionPane.showMessageDialog(null, "Closing Program");
            System.exit(0);
       }
       inputFile = selecter.getSelectedFile();
       JOptionPane.showMessageDialog(null, "Opening: " + inputFile.getName());
       in = new Scanner(inputFile);
       int[] temps = new int[161];
       int num = in.nextInt();
       int base10 = 0;
       while (in.hasNextInt());
       {
           temps[num]++;
           num = in.nextInt();
       }
       for (int count = 1; count <= 160; count += 10)
       {
           System.out.print(count + " - " + (base10 += 10) + " | " );
           for (int index = count; index <= base10; index++)
           {
               while (temps[index] > 0)
               {
                   System.out.print("*");
                   temps[index]--;
               }
           }
           System.out.println();
       }
       in.close();
    }
}

When I run the program and select my input file, it gives me this message:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at CoolWeather.main(CoolWeather.java:30)
Al-un
  • 3,102
  • 2
  • 21
  • 40
DrtyG
  • 5
  • 3
  • Whats you sample input to the above program? – Naman Oct 15 '17 at 20:51
  • Line 30 is `num = in.nextInt();`. Most likely, you have non numerical characters in your file – Al-un Oct 15 '17 at 20:53
  • The problem is that you don't check in.hasNextInt() before using int num = in.nextInt(); So if there's no integer in the input stream at all, Java will throw a InputMismatchException. You can prevent this, as you're already doing later in the code, by checking in.hasNextInt() before calling in.nextInt(). (Basically the same as Al1's comment) – M_F Oct 15 '17 at 20:54
  • I forgot to use a delimiter to extract just the numbers from my input file. I just added it and it got everything working properly. Now my next problem is the FileNotFoundException that pops up when I select a file that I intentionally deleted just to test the "throws FileNotFoundException" statement that i added after "main(String[] args)". I'm still learning what that does cause I was under the impression that it would prevent this kind of exception. – DrtyG Oct 15 '17 at 21:15

0 Answers0