2 Replies - 177 Views - Last Post: 05 February 2012 - 01:50 AM Rate Topic: ***** 1 Votes

Topic Sponsor:

#1 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 06-January 12

error with .set for ArrayList

Posted 05 February 2012 - 01:01 AM

So Im working on this trivia game, and my syntax is whack, and giving me this error
--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

Is This A Good Question/Topic? 0
  • +

Replies To: error with .set for ArrayList

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: error with .set for ArrayList

Posted 05 February 2012 - 01:15 AM

What is the purpose of getPlayerData() method? What I see there you are setting null value to non existing index in an empty list!!!!
Again you call setName() to a null player...
get() throw out of bounds exception if index is not between 0 and size of the list. But your list size is 0, so simply you cant use set with index parameter. Use add() to add an element to a list
Was This Post Helpful? 1
  • +
  • -

#3 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 06-January 12

Re: error with .set for ArrayList

Posted 05 February 2012 - 01:50 AM

ha wow yea that was dumb,

player.add(new Player(in.nextLine()));


^That should work right

You have any thoughts on my problem with distributing the questions evenly, and removing the extra questions?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1