-1

I'm having trouble figuring out what's wrong with my java code. I am creating a Trivia Game that reads in the question ID, question, answer, and answer point value from a dat file.

I've tried all sorts of things, but am getting the same NumberFormatException.

Below is an example of how the dat file is setup: 10 questions total

1: 01 2: What is light as a feather, but even the strongest man cannot hold it more than a few minutes? 3: His breath 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)
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    First off change quest.setQuestionID(Integer.valueOf(br.readLine())); to be quest.setQuestionID(Integer.valueOf(stringLine));, and everywhere else within your while loop use stringLine instead of br.readLine()); – CAMD_3441 May 02 '19 at 01:52
  • 1
    Secondly are you sure that stringLine is always an Integer? – CAMD_3441 May 02 '19 at 01:53
  • Hey, thanks for responding! The dat file switches from integers to strings so I am trying to read in both types with the bufferedreader. Would changing stringLine to a char type fix? – danasaurus_rex May 02 '19 at 02:00
  • Changing it to stringLine won't fix the issue. But the while loop you're returning the files contents read in from the br.readLine() to stringLine, so within the while loop you only need to use stringLine. You still need to check if the contents of stringLine is an Integer before calling Integer.valueof(stringLine) – CAMD_3441 May 02 '19 at 02:05
  • [tag:bufferedreader] has nothing to do with it. See the stack trace. – user207421 May 02 '19 at 02:57

1 Answers1

0

In addition to what I mentioned above you can use StringUtils.isNumeric method to see if stringLine only contains numeric values.

You can find that method with Apache Commons Lang dependency.

if you're using maven here's the link to download it to your project https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.5

If you're not using maven you should be able to download the jar from that link (or from https://jar-download.com/artifacts/org.apache.commons/commons-lang3/3.5/source-code) and then include the jar as a library in your project.

CAMD_3441
  • 2,514
  • 2
  • 23
  • 38
  • I just found this other stack overflow question that shows various ways to see if a string is an integer https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java you should look into this and do these checks in your code. – CAMD_3441 May 02 '19 at 02:12