Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help asap!! My code runs fine, but I can\'t display the question correctl

ID: 655410 • Letter: I

Question

I need help asap!!
My code runs fine, but I can't display the question correctly and see if the correct answer was chosen. This is a trivia game. 2 players, each asked 10 questions, they have to choose the right answer that's in my text file. Here's my main class, followed by player.java and question.java.


package trivia;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;



public class TriviaGame
{
   public static void main(String args[]) throws IOException
   {
       // Constants
       final int NUM_QUESTIONS = 10;
       final int NUM_PLAYERS = 2;

       // Variables
       int playerTurn = 1; // The current player
       int questionNum; // The current question number
       int playerAnswer; // The player's chosen answer
       int player1points = 0; // Player 1's points
       int player2points = 0; // Player 2's points

       // Create an array of Player objects for player #1 and player #2.
       Player[] players = new Player[NUM_PLAYERS];
       for (int i = 0; i < NUM_PLAYERS; i++)
       {
           players[i] = new Player(i+1);
       }

       // Create an array to hold Question objects.
           Question[] question = new Question [NUM_QUESTIONS];

       // Initialize the array with data.
       initQuestions(question);

       // Play the game.
       for (int i = 0; i < NUM_QUESTIONS; i++)
       {
           // Display the question.
/*
           TriviaGame.displayQuestion(qArray[i], playerTurn);
*/
           // Get the player's answer.
           players[playerTurn - 1].chooseAnswer();

           // See if the correct answer was chosen.
           /*
if (qArray[i].getCorrectAnswerNumber() == players[playerTurn - 1].getCurrentAnswer())
           {
               players[playerTurn -1].incrementPoints();
           }
*/

           // Switch players for the next iteration.
           if (playerTurn == 1)
               playerTurn = 2;
           else
               playerTurn = 1;
       }

       // Show the game results.
       showGameResults(players);
   }

   /**
   * The initQuestions method uses the contents of the trivia.txt file to
   * populate the qArray parameter with Question objects.
   */
   public static void initQuestions(Question qArray[]) throws IOException
   {
       // Open the trivia.txt file.
       File file = new File("trivia.txt");
       Scanner inputFile = new Scanner(file);

       // Populate the qArray with data from the file.
       for (int i = 0; i < qArray.length; i++)
       {
           // Create a Question object in the array.
           qArray[i] = new Question();

           // Get the question text from the file.
           qArray[i].setQuestion(inputFile.nextLine());

           // Get the possible answers.
           for (int j = 1; j <= 4; j++)
           {
               qArray[i].setPossibleAnswer(inputFile.nextLine(), j);
           }

           // Get the correct answer.
           qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));
       }
   }

   public static void displayQuestion(Question q, int playerNum)
   {
       // Display the player number.
       System.out.println("Question for player #" + playerNum);
       System.out.println("------------------------");

       // Display the question.
       System.out.println(q.getQuestionText());
       for (int i = 1; i <= 4; i++)
       {
           System.out.println(i + ". " + q.getPossibleAnswer(i));
       }
   }

   public static void showGameResults(Player[] players)
   {
       // Display the stats.
       System.out.println("Game Over!");
       System.out.println("---------------------");
       System.out.println("Player 1's points: " + players[0].getPoints());
       System.out.println("Player 2's points: " + players[1].getPoints());

       // Declare the winner.
       if (players[0].getPoints() > players[1].getPoints())
           System.out.println("Player 1 wins!");
       else if (players[1].getPoints() > players[0].getPoints())
           System.out.println("Player 2 wins!");
       else
           System.out.println("It's a TIE!");
   }
}

PLAYER CLASS:


package trivia;

import java.util.Scanner;

//SECOND CLASS
public class Player
{
   private int playerNumber; // The player number
   private int points; // Player's points
   private int currentAnswer; // Current chosen answer

   //Constructor
   public Player(int playerNum)
   {
       playerNumber = playerNum;
       points = 0;
   }

   public void chooseAnswer()
   {
       // Create a Scanner object for keyboard input.
       // Get the user's chosen answer.
   Scanner input = new Scanner(System.in);
   System.out.print("Please enter your Answer");
   currentAnswer = input.nextInt();
   }

   public int getCurrentAnswer()
   {
       return currentAnswer;
   }

   public void incrementPoints()
   {
       points++;
   }

   public int getPoints()
   {
       return points;
   }

}

THIRD CLASS:


package trivia;


public class Question
{
   // Constant for the number of answers
   public final int NUM_ANSWERS = 10;

   // The trivia question
   private String questionText;

    // An array to hold possible answers.
    private String possibleAnswers[] = new String[NUM_ANSWERS];

    // The number (1, 2, 3, or 4) of the correct answer.
    private int correctAnswer;

   //Constructor
   public Question()
   {
       // Initialize all fields to "" or 0;
       questionText = "";
       correctAnswer = 0;
       for (int i = 1; i < NUM_ANSWERS; i++)
           setPossibleAnswer("", i);
   }

   public void setQuestion(String question)
   {
       questionText = question;           //Sets the question
   }

   public void setPossibleAnswer(String text, int num)
   {
       possibleAnswers[num] = text;       //Sets possible Answer
   }

   public void setCorrectAnswerNumber(int num)
   {
       correctAnswer = num;               //Sets correct Answer
   }

   public String getQuestionText()
   {
       return questionText;               //Returns Question Text
   }

   public String getPossibleAnswer(int num)
   {
       return possibleAnswers[num];       //Returns Possible Amswer
   }

   public int getCorrectAnswerNumber()
   {
       return correctAnswer;               //Returns Correct Answer
   }

   public String getCorrectAnswer()
   {
       return possibleAnswers[this.correctAnswer];   //Returns Possible Answer
   }
}

Explanation / Answer

import java.util.Scanner;

import java.io.*;

import java.util.ArrayList;

public class Question {

    private static int NumberofQuest = 10;

    private String[] question = new String[NumberofQuest] ;

    private static int NumofAns=0;

    private String[] possibleAns = new String[NumofAns];

    private int[] correctAns = new int[NumberofQuest];

    public Question()

    {

        question= null;

        for (int i=0; i<= NumofAns; i++)

        {

            setPossibleAns("");

        }

        correctAns=null;

    }

   

   public static ArrayList<Object> createquestion(String filename) throws IOException

   {

       Question qArray[] = null;

       File file = new File(filename);

       Scanner line = new Scanner(file);

      

       ArrayList<Object> qObject = new ArrayList<>();

      while (line.hasNext())

      {

      

       for (int i=0; i<= NumberofQuest; i++)

       {

           qArray[i] = new Question();

           qArray[i].setQuestion(line.nextLine());

           qArray[i].setNumofAns(Integer.parseInt(line.nextLine()));

           for (int j=0; j<= NumofAns; j++){

               qArray[i].setPossibleAns(line.nextLine());

           }

           qArray[i].setNumofAns(Integer.parseInt(line.nextLine()));

           qObject.add(qArray[i]);

       }

    

      }

        return qObject;

   }

   

    public void setQuestion(String quest)

   {

       question[NumberofQuest]= quest;

   }

   public String[] getQuestion()

   {

       return question;

   }

   public void setNumofAns(int num)

   {

       NumofAns=num;

   }

   public int getNumofAns()

   {

       return NumofAns;

   }

   public void setPossibleAns(String ans)

   {

       possibleAns[NumofAns]= ans;

   }

   public String[] getPossibleAns()

   {

       return possibleAns;

   }

   public void setCorrectAns(int correctNum)

   {

       correctAns[NumberofQuest]= correctNum;

   }

   public int[] getCorrectAns()

   {

       return correctAns;

   }

   public static void displayQuestion(Question q, int playerTurn) throws IOException

   {

       System.out.println("Question for player #" + playerTurn);

       System.out.println(q.getQuestion());    

       for (int i=0; i <= NumofAns; i++)

       {

           System.out.println(q.getPossibleAns());

       }

   }

  

  

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote