【问题标题】:Input Mismatch scanner.next(string pattern) from file从文件中输入不匹配的scanner.next(字符串模式)
【发布时间】:2015-01-29 01:22:02
【问题描述】:

我在使用 扫描仪文件 = 新扫描仪(文件路径); 我的文件是这样组织的

问题 x.y
答案1
答案2
答案3
答案4
提示:提示问题 x.y 级别 z Genere t

我对 java 很陌生,在我使用过的所有项目中 .next(字符串模式) 返回一个包含 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);


}

【问题讨论】:

    标签: file java.util.scanner next inputmismatchexception


    【解决方案1】:

    尝试添加

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

    扫描仪文件之前

    【讨论】:

      猜你喜欢
      • 2012-11-24
      • 1970-01-01
      • 2019-03-05
      • 2013-07-11
      • 2014-10-19
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多