NumberFormatException错误-从文件读取并将输入解析为数字

时间:2019-05-02 01:46:32

标签: java numberformatexception

我很难弄清楚我的Java代码有什么问题。我正在创建一个Trivia游戏,该游戏从dat文件中读取问题ID,问题,答案和答案点的值。

我已经尝试过各种方法,但是却得到了相同的NumberFormatException

下面是一个如何设置dat文件的示例:总共10个问题

1:01 2:轻如羽毛,但即使最坚强的人也无法忍受    比几分钟? 3:他的呼吸 4:3

Game.java

import java.util.*;
import java.io.*;

public class Game {

    // Instance Variables

    private QuestionBank[] questions;
    private int numQuestions;
    private int questionNumber;
    private int playerScore;

    // Constructor

    public Game()
    {
        QuestionBank[] questions = new QuestionBank[10];
        numQuestions = 0;
        questionNumber = 0;
        playerScore = 0;
    }

    public Game(FileInputStream questionsFile)
    {

        BufferedReader br = new BufferedReader(new InputStreamReader(questionsFile));
        String stringLine = null;
        int i = 0;

        try
        {

            while((stringLine = br.readLine()) != null)
            {
                QuestionBank quest = new QuestionBank();
                quest.setQuestionID(Integer.valueOf(br.readLine())); //ERROR OCCURS HERE
                quest.setQuestion(br.readLine());
                quest.setAnswer(br.readLine());
                quest.setPointValue(Integer.valueOf(br.readLine()));

                questions[i] = quest;
                i++;
                stringLine = null;
            }
            br.close();
        } 
        catch (IOException e)
        {
            System.out.println("Uh oh. Exception caught.");

        }

        this.questionNumber = 0;

        /*Scanner questionsFileScanner = new Scanner(questionsFile);
        questions = new QuestionBank[5];

        while(questionsFileScanner.hasNextLine())
        {
            for(int i = 0; i < 4; ++i)
            {
                questions[i] = new QuestionBank();
                questions[i].setQuestion(questionsFileScanner.nextLine());

            }

        }*/

    }

    //Accessors and Mutators

    public int getNumQuestions()
    {
        return numQuestions;
    }

    public int getQuestionNumber()
    {
        return questionNumber;
    }

    public int getPlayerSocre()
    {
        return playerScore;
    }


    public boolean checkAnswer(String answer)
    {
        if(answer.contentEquals(questions[questionNumber].getAnswer()) == true)
        {
            playerScore += questions[questionNumber].getPointValue();
            ++questionNumber;
            return true;
        }

        else
        {
            return false;
        }

    }

    public String getNextQuestion()
    {
        return questions[questionNumber].getQuestion();
    }

    public String toString()
    {
        String outputString = "";
        for (int i = 0; i < questionNumber; ++i)
        {
            outputString = questions[i].toString();
        }

        return outputString;
    }
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "What is light as a feather, but even the strongest man cannot hold it more than a few minutes?"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.valueOf(Integer.java:983)
    at project7.Game.<init>(Game.java:41)
    at project7.RunGame.main(RunGame.java:41)

1 个答案:

答案 0 :(得分:0)

除了我上面提到的内容之外,您还可以使用StringUtils.isNumeric方法查看stringLine是否仅包含数字值。

您可以找到具有Apache Commons Lang依赖项的方法。

如果您使用的是maven,则这里是将其下载到项目https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.5

的链接

如果您不使用Maven,则应该能够从该链接(或从https://jar-download.com/artifacts/org.apache.commons/commons-lang3/3.5/source-code)下载jar,然后将该jar作为库包含在您的项目中。