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)