Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Trying to build trivia game with questions from a file, having some issues

I am trying to build a 2-4 player trivia game using questions from a text file and I am stuck on getting the questions from the text file to add to my array list. It seems like I can only get the first question to add and no matter where I try to change my counter, I get an out of bound exception. Can anyone give me some insight on how to make this work? I want to be able to move to the next question.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Questions {

    private ArrayList<String> theQuestions;
    private int current; //current question you're asking
    private int next;


    public Questions(){ //make this the default constructor
        current = 0; //haven't asked any questions yet

        String fileName = "triviaquestions.txt";
        File inputFile = new File(fileName);
        Scanner inFile = null;
        String[] pieces = null;

        //Checked exception - java is forcing you to deal with this.
        try {
            inFile = new Scanner(inputFile);
        }
        catch (FileNotFoundException ex){
            System.out.println("No such file exists.");
            System.exit(0);
        }

        //setup arrayList to hold questions
        theQuestions = new ArrayList<>();        

        try {
            while (inFile.hasNextLine()){
                String line = inFile.nextLine();
                //Only adding first line of file to arrayList...why?
                theQuestions.add(line);
            }
        }
        catch (ArrayIndexOutOfBoundsException ex){
            System.out.println("The file format is incorrect.");
        }
        catch (InputMismatchException ex){
            System.out.println("The file format is incorrect.");
        }
        finally {
            inFile.close();
        }
    }

    //This splits the first question, how do I get to the next question?
    public void getQuestion(){
        //Split question here
        System.out.println(theQuestions.get(current));
        String[] pieces = theQuestions.get(current).split("\\|");

        for (int i = 0; i <= 4; i++){
            System.out.println(pieces[i]);
        }
    }

    public void theAnswer(){
        System.out.println(theQuestions.get(current));
        String[] pieces = theQuestions.get(current).split("\\|");

        for (int i = 0; i <=0; i++){
            String answer = pieces[5];
            System.out.println(answer);
        }
    }
}