• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It just might be that the file starts with a Byte order mark.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic