So, I am working on a code for class and can't figure what is wrong. The code compiles and when I enter the file I'm searching for, I get this message:
Enter the name of the file: FanData.txt
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at FanDriver.fillArray(FanDriver.java:76)
at FanDriver.main(FanDriver.java:35)
Press any key to continue . . .
I'm using TextPad as my compiler and the text file is in the project. The following is the code that I have written (ignore the methods being called in the quotes as they are something I need to do afterwards):
import java.io.*;
import java.util.Scanner;
public class FanDriver
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) throws IOException
{
// Constant for the amount of elements of the array
final int MAXSIZE = 100;
// Declaring variables
int amountFans = 0;
// Declaring and initializing our array of fans
Fan[] fans = new Fan[MAXSIZE];
// Calling all of our methods
amountFans = fillArray(fans, MAXSIZE);
/**
listFanData(fans, amountFans);
bubbleSortByAge(fans, amountFans);
listFanData(fans, amountFans);
bubbleSortByFan(fans, amountFans);
listFanData(fans, amountFans);
searchByAge(fans, amountFans);
searchByFan(fans, amountFans);
*/
}
public static int fillArray(Fan[] array, int MAXSIZE) throws IOException
{
// Declaring variables
int counter = 0;
int age;
String name;
// Getting the file name
System.out.print("\nEnter the name of the file: ");
String fileName = keyboard.nextLine();
// Opening the file
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
// Making sure the file was successfully opened
if (!file.exists())
{
System.out.println("\nERROR: FILE DOESN'T EXIST. CLOSING PROGRAM NOW.");
// Exiting the program
System.exit(0);
}
// Reading all of the amounts from the file
while (inputFile.hasNext() && counter < MAXSIZE)
{
name = inputFile.nextLine();
age = inputFile.nextInt();
array[counter] = new Fan(name, age);
// Adding to our counter
counter = counter + 1;
}
//Closing file
inputFile.close();
return counter;
}
}
I do not have the code for the Fan class, just the class itself. The file we are retrieving is the file FanData.txt, which looks like this:
Chris P. Cream 5 Scott Free 9 Lou Tenant 3 Trish Fish 12 Ella Mentry 4 Holly Day 3 Robyn DeCradle 12 Annette Funicello 4 Elmo 7 Grover 3 Big Bird 9 Bert 7 Ernie 3 Grover 9
The text file is line-by-line. One line is a name and the next is a number. I don't know how to format it correctly on here. Any help would be greatly appreciated.
I am not sure about the delimiter in your input file.
The way you had instantiated your "Scanner"(without specifying any explicit delimiter) shall use default delimiter i.e. a whitespace character.
It seems that the "nextLine()" method in your case is not returning a "line".
It is rather returning a number or string(may be, you need check your file for that) which is not matching with the pattern followed by your Scanner.