--Also in the playGame method, I want the program to take a number of questions like 11 with a number of player like 3, and get a number like 3 questions for each user, whats the correct syntax for rounding?
"Enter filename for questions and answers:
lab3.txt
Enter number of players:
2
Exception in thread "main" Enter name for Player 1:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.set(Unknown Source)
at Lab3.getPlayerData(Lab3.java:45)
at Lab3.main(Lab3.java:20) "
..my code is..
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Lab3
{
public static void main(String [] args)
{
try
{
Scanner in = new Scanner(System.in);
Scanner inFile = getFile(in);
System.out.println("Enter number of players: ");
int numPlayer = in.nextInt();
ArrayList<Player> player = new ArrayList<Player>();
ArrayList<Question> questions = new ArrayList<Question>();
getPlayerData(in, numPlayer, player);
readFile(inFile, questions);
playGame(numPlayer, questions, player);
inFile.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
public static Scanner getFile(Scanner in) throws IOException
{
System.out.println("Enter filename for questions and answers: ");
String fileName = in.nextLine();
File file = new File(fileName);
Scanner inFile = new Scanner(file);
return inFile;
}
public static void getPlayerData(Scanner in, int numPlayer, ArrayList<Player> player) throws IOException
{
for(int i = 0;i < numPlayer;i++)
{
System.out.println("Enter name for Player "+(i+1)+": ");
player.set(i, null).setName(in.nextLine());
}
}
public static void readFile(Scanner inFile, ArrayList<Question> questions)
{
while (inFile.hasNextLine())
{
Question q = new Question();
String question = inFile.nextLine().trim();
q.setQuestion(question);
ArrayList<String> answers = q.getAnswers();
String answer = null;
boolean startNextQuestion = false;
int answerIndex = 0;
while (!startNextQuestion && inFile.hasNextLine())
{
answer = inFile.nextLine().trim();
if (!"-".equals(answer))
{
if (answer.startsWith("*"))
{
answer = answer.substring(1);
}
answers.add(answer);
answerIndex++;
}
else
{
startNextQuestion = true;
}
}
questions.add(new Question(question, answers, answerIndex));
}
}
public static void playGame(int numPlayer, ArrayList<Question> questions, ArrayList<Player> player)
{
System.out.println("I have been provided "+questions.size()+" questions.");
int numQuestionsAsked = questions.size();
/*if((index / numPlayer) == 0) // was trying to take an uneven amount of amount of questions and distribute among players(i.e. 7 questions for 3 players = 2 questions each)
{
questions.remove(index);
index--;
}
numQuestionsAsked = index / numPlayer; */
System.out.println("Each player will be asked "+numQuestionsAsked+" of them.");
System.out.println("The player with the highest score will be named the winner.");
int p = 0;
for(int x = 0;x < numQuestionsAsked;x += numPlayer)
{
for(int y = 1;y < numPlayer;y++)
{
int choice = -1;
System.out.println(player.get(y).getName() + ": "+ questions.get(p).getQuestion()); //
if(choice == questions.get(p).getCorrectAnswer()) // ... trying to access elements of a Question ArrayList(String Question and ArrayList posAnswers)
{
player.get(y).increaseScore();
}
p++;
}
}
}
}
the area with the rounding I actually commented out, sorry for any confusion

New Topic/Question
Reply




MultiQuote




|