Input Mismatch scanner.next(string pattern) from file

145 views Asked by At

I'm having a problem while using a Scanner file = new Scanner(filePath); my file is organised as so

Question x.y
Answer1
Answer2
Answer3
Answer4
Hint: Hint to Question x.y Level z Genere t

I'm quite new to java and in all of this project I've used .next(string pattern) to return a string that includes the parrtern and arrives to the end of the line but i continue to get mismatch exceptions.. i don't really get what https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html defines as token and for this reason i don't know how to handle it..

my code is

public Question getQuestion(String filePath)
{
    ArrayList questions = new ArrayList();

    Scanner file = new Scanner(filePath);
    String questionInFile = "Question";
    while(file.hasNextLine())
        questions.add(questionInFile+file.next(questionInFile));
    Random rg = new Random();
    String Q = (String)questions.get(rg.nextInt(10)+1);
    file.close();
    ArrayList<String> answer=getAnswers(filePath, Q);
    String correctAnswer=answer.get(0);

    return new Question(Q, answer, correctAnswer);


}
1

There are 1 answers

0
Anastasia Smirnova On

try adding

File filePath = new File(".txt");

before Scanner file