• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot figure out why I am getting "java.util.InputMismatchException"  RSS feed

 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
....may be first token in your file is space and not integer 35.

Manish
 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip. I will check that out.
 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at the file and it looks like there is no space before the "35".. Any other suggestions? Im still scratching my head...
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just might be that the file starts with a Byte order mark.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!