I am having issues with the following lines of code: 62 - public static void ini
ID: 3696168 • Letter: I
Question
I am having issues with the following lines of code:
62 - public static void initQuestions(Question questionsArray[]) throws IOException
84 - public static void displayQuestion(Question q, int playerNum)
98 - public static void showGameResults(Player[] players)
The errors I am still getting
Line
62
Multiple markers at this line
- Syntax error, insert ";" to complete
LocalVariableDeclarationStatement
- Syntax error on token "(", ; expected
- void is an invalid type for the variable initQuestions
84
Multiple markers at this line
- Syntax error on token ")", ;
expected
- Syntax error on token ",", ;
expected
- Syntax error on token "(", ;
expected
98
Multiple markers at this line
- Syntax error on token "(", ;
expected
- Syntax error on token ")", ;
expected
the complete code from my program:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class TriviaDriver {
public static void main(String args[]) throws IOException
{
// Constants
final int NUM_QUESTIONS = 10;
final int NUM_PLAYERS = 2;
// Variables
int playerTurn = 1;
int questionNum;
int playerAnswer;
int player1points = 0;
int player2points = 0;
//array of Player objects for player #1 and player #2.
Player[] players = new Player[NUM_PLAYERS];
//player for loop
for (int i = 0; i < NUM_PLAYERS; i++)
{
players[i] = new Player(i+1);
}
// array to hold questions
Question[] questions = new Question[NUM_QUESTIONS];
initQuestions(questions);
// Play the game.
for (int i = 0; i < NUM_QUESTIONS; i++)
{
{
//print the Trivia object question
Object[] questionsArray;
TriviaDriver.displayQuestion(questionsArray[i], playerTurn);
//prompt user for answer
players[playerTurn - 1].chooseAnswer();
if(((Question) questionsArray[i]).getCorrectAnswerNumber() == players[playerTurn - 1].getCurrentAnswer())
{ //increment score
players[playerTurn - 1].incrementPoint();
}
if(playerTurn == 1)
playerTurn = 2;
else
playerTurn = 1;
}
//show results
showGameResults(players);
/* the initQuestions method uses contents of trivia.text file to populate aArray
*/
public static void initQuestions(Question questionsArray[]) throws IOException
{
//open trivia file
File file = new File("trivia.txt");
Scanner inputFile = new Scanner(file);
//fill the qArray
for(int i = 0; i <questionsArray.length; i++)
{
qArray[i] = new Question();
qArray[i].setQuestion(inputFile.nextLine());
for(int j = 1; j<= 4; j++)
{
qArray[i].setPossibleAnswer(inputFile.nextLine(),j);
}
//get right answer
qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));
}
}
public static void displayQuestion(Question q, int playerNum)
{
//show player number
System.out.println("Question for player" + playerNum);
System.out.println("----------------");
//show 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)
{
//show the points
System.out.println("End of game");
System.out.println("----------------");
System.out.println("Player 1's points: " + players[0].getPoints());
System.out.println("Player 2's points: " + players[1].getPoints());
//who won the game??
if(players[0].getPoints() > players[1].getPoints())
System.out.println("Player 1 is the winner");
else if(players[0].getPoints() < players[1].getPoints())
System.out.println("Player 2 is the winner");
else
System.out.println("You tied");
}
}
}
private static void showGameResults(Player[] players) {
// TODO Auto-generated method stub
}
private static void displayQuestion(Object object, int playerTurn) {
// TODO Auto-generated method stub
}
private static void initQuestions(Question[] questions) {
// TODO Auto-generated method stub
}
}
Explanation / Answer
Hi, I have modified your code. It is working now.
TriviaDriver.java
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class TriviaDriver {
public static void main(String args[]) throws IOException
{
// Constants
final int NUM_QUESTIONS = 10;
final int NUM_PLAYERS = 2;
// Variables
int playerTurn = 1;
int questionNum;
int playerAnswer;
int player1points = 0;
int player2points = 0;
//array of Player objects for player #1 and player #2.
Player[] players = new Player[NUM_PLAYERS];
//player for loop
for (int i = 0; i < NUM_PLAYERS; i++)
{
players[i] = new Player(i+1);
}
// array to hold questions
Question[] questions = new Question[NUM_QUESTIONS];
initQuestions(questions);
// Play the game.
for (int i = 0; i < NUM_QUESTIONS; i++)
{
{
//print the Trivia object question
Object[] questionsArray;
TriviaDriver.displayQuestion(questionsArray[i], playerTurn);
//prompt user for answer
players[playerTurn - 1].chooseAnswer();
if(((Question) questionsArray[i]).getCorrectAnswerNumber() == players[playerTurn - 1].getCurrentAnswer())
{ //increment score
players[playerTurn - 1].incrementPoint();
}
if(playerTurn == 1)
playerTurn = 2;
else
playerTurn = 1;
}
//show results
showGameResults(players);
}
} //main close
/* the initQuestions method uses contents of trivia.text file to populate aArray
*/
public static void initQuestions(Question questionsArray[]) throws IOException
{
//open trivia file
File file = new File("trivia.txt");
Scanner inputFile = new Scanner(file);
//fill the qArray
for(int i = 0; i <questionsArray.length; i++)
{
qArray[i] = new Question();
qArray[i].setQuestion(inputFile.nextLine());
for(int j = 1; j<= 4; j++)
{
qArray[i].setPossibleAnswer(inputFile.nextLine(),j);
}
//get right answer
qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));
}
}
public static void displayQuestion(Question q, int playerNum)
{
//show player number
System.out.println("Question for player" + playerNum);
System.out.println("----------------");
//show 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)
{
//show the points
System.out.println("End of game");
System.out.println("----------------");
System.out.println("Player 1's points: " + players[0].getPoints());
System.out.println("Player 2's points: " + players[1].getPoints());
//who won the game??
if(players[0].getPoints() > players[1].getPoints())
System.out.println("Player 1 is the winner");
else if(players[0].getPoints() < players[1].getPoints())
System.out.println("Player 2 is the winner");
else
System.out.println("You tied");
}
} // class end
The changes what i did here is, removed all three methods from mail() method and places them outside of main method and with in class. You can not implement methods() with in anther methods().
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.