Hey All,
I’ve searched on these forums, and others, to no avail. I need your help. I am at the end of the lesson, Step 21:
"Try it out!
If you’ve completed every step, Unquote is now… partially functional!
Tab over to Main.java
to “play” the game:
- Start the game by invoking
MainActivity.startNewGame();
- Retrieve the current
Question
by callingMainActivity.getCurrentQuestion();
- Print the
Question
and answers using the providedprintQuestion()
method - Set an answer directly on the
Question
by modifying theplayerAnswer
member variable - Then submit an answer by invoking
MainActivity.onAnswerSubmission();
- Go nuts!"
From what I have seen online, the code should work at this point and print the question, at least. But, my code isn’t doing even that. Without doing the steps 4-6, I am getting this error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:440) at MainActivity.chooseNewQuestion(MainActivity.java:33) at MainActivity.startNewGame(MainActivity.java:24) at Main.main(Main.java:4)
Here’s my Code Currently:
Question.java
:
public class Question {
int imageId;
String questionText;
String answer0;
String answer1;
String answer2;
String answer3;
int correctAnswer;
int playerAnswer = 1;
public Question(int imageIdentifier,
String questionString,
String answerZero,
String answerOne,
String answerTwo,
String answerThree,
int correctAnswerIndex) {
imageId = imageIdentifier;
questionText = questionString;
answer0 = answerZero;
answer1 = answerOne;
answer2 = answerTwo;
answer3 = answerThree;
correctAnswer = correctAnswerIndex;
playerAnswer = 1;
}
boolean isCorrect() {
return correctAnswer == playerAnswer;
}
}
And my MainActivity.java
:
import java.util.ArrayList;
public class MainActivity {
// TODO #1: add integer member variables here
int currentQuestionIndex;
int totalCorrect;
int totalQuestions;
// TODO #2: add ArrayList member variable here
ArrayList<Question> questions = new ArrayList<Question>();
// TODO #3 add startNewGame() here
void startNewGame() {
Question question1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
Question question2 = new Question(107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace", 3);
Question question3 = new Question(748294, "What is the name for the patch of skin found on your elbow?", "Elbow Skin", "Fascia Elbora", "Wenis", "Todd", 2);
ArrayList<Question> questions = new ArrayList<Question>();
questions.add(question1);
questions.add(question2);
questions.add(question3);
totalCorrect = 0;
totalQuestions = questions.size();
Question firstQuestion = chooseNewQuestion();
// displayQuestion(firstQuestion);
// displayQuestionsRemaining(questions.size());
}
// TODO #4 add chooseNewQuestion() here
Question chooseNewQuestion() {
int randomNum = generateRandomNumber(2);
currentQuestionIndex = randomNum;
return questions.get(currentQuestionIndex);
}
// TODO #5 add getCurrentQuestion() here
Question getCurrentQuestion() {
return questions.get(currentQuestionIndex);
}
// TODO #6 add onAnswerSubmission() here
void onAnswerSubmission() {
Question currentQuest = getCurrentQuestion();
if (currentQuest.isCorrect()) {
totalCorrect =+ 1;
questions.remove(currentQuest);
}
//displayQuestionsRemaining(questions.size());
if(questions.size() == 0) {
System.out.println("Game Over");
startNewGame();
chooseNewQuestion();
// TODO: uncomment after implementing displayQuestion()
// displayQuestion(getCurrentQuestion());
}
}
int generateRandomNumber(int max) {
double randomNumber = Math.random();
double result = max * randomNumber;
return (int) result;
}
String getGameOverMessage(int totalCorrect, int totalQuestions) {
if (totalCorrect == totalQuestions) {
return "You got all " + totalQuestions + " right! You won!";
} else {
return "You got " + totalCorrect + " right out of " + totalQuestions + ". Better luck next time!";
}
}
public static void main(String[] args) {
System.out.println("Test: Working");
}
}
And, lastly, my Main.java
:
public class Main {
public static void main(String[] args) {
MainActivity newMainActivity = new MainActivity();
newMainActivity.startNewGame();
System.out.println("Questions remaining: " + newMainActivity.totalQuestions);
Question currentQuestion = newMainActivity.getCurrentQuestion();
printQuestion(currentQuestion);
// Play the game!
}
static void printQuestion(Question question) {
System.out.println("Question: " + question.questionText);
System.out.println("Option 1: " + question.answer0);
System.out.println("Option 2: " + question.answer1);
System.out.println("Option 3: " + question.answer2);
System.out.println("Option 4: " + question.answer3);
}
}
From what I can tell, the error is pointing out a mismatch in the ArrayList.size() and some method’s calling the wrong index location. That, or one that doesn’t exist? However, when I looked through the code it points to, it matches what others have used and have working on theirs they posted here.
This lesson has been great for the most part. However, when it suddenly goes from individual components, like learning loops or arrays, to something like this, putting it all together, and having it span across multiple .java files or classes, it gets very confusing and difficult to debug. I think this lesson and cumulative ones like it, need a lot more support and explanations. These forums are also no help directly, because there are too many potential issues, for me to find the exact one causing my error message. This course isn’t free. I would prefer not to have to wait a week for an answer, while paying to use Pro.
What Am I Missing? Please Help!