Hi,
I keep getting a java.util.inputMismatchException while trying to open and read from a simple text file. I believe the exception is thrown because of a mismatch between what is in the file and what is being expected. The file contents are: 35 question A choicea choiceb choicec choiced . It looks like the error is being thrown when trying to read "35" from the file. It should be read into qnumber which is an int. The code im trying to use is as follows:
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class tData {
private int key;
private int qNumber;
private
String question;
private String answer;
private String choicea;
private String choiceb;
private String choicec;
private String choiced;
private String[] nquestion = new String[6];
public tData(int key){
this.key=key;
}
public String[] sendQuestions(){
try {
Scanner inFile = new Scanner(new FileReader("trivia.txt"));
while (inFile.hasNext())
{
qNumber = inFile.nextInt();
question = inFile.next();
answer = inFile.next();
choicea = inFile.next();
choiceb = inFile.next();
choicec = inFile.next();
choiced = inFile.next();
if (key == qNumber)
{
JOptionPane.showMessageDialog(null,"Key and qnumber are" + key + qNumber ,null, JOptionPane.INFORMATION_MESSAGE);
inFile.close();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return nquestion;
}
}
The line number referenced with the exception is pointing to this: qNumber = inFile.nextInt();
I know it is probably something very simple, but I am just not seeing it. Any help would be greatly appreciated... thanks!