0

我在使用扫描仪时遇到问题 file = new Scanner(filePath); 我的文件是这样组织的

问题 xy
Answer1 Answer2
Answer3
Answer4
提示
:提示问题 xy 级别 z Genere t

我对 java 很陌生,在所有这个项目中,我都使用 .next(string pattern) 返回一个包含 parrtern 并到达行尾的字符串,但我继续收到不匹配异常。我不知道'并没有真正得到 https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html 定义为令牌的内容,因此我不知道如何处理它..

我的代码是

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);


}
4

1 回答 1

1

尝试添加

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

扫描仪文件之前

于 2014-12-01T20:36:27.293 回答