Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayIndexOutOfBoundsException In Simon Game, Can't Figure Out Why
    primarykey
    data
    text
    <p>I'm pretty new to Java, but not OOP in general. I'm working on the classic Simon game where the user follows an increasing sequence of a randomly generated pattern (numbers really) for my Java class. I'm getting an error: </p> <pre><code>java.lang.ArrayIndexOutOfBoundsException: 20 at Simon.generateSequence(Simon.java:58) at Simon.&lt;init&gt;(Simon.java:23) </code></pre> <p>I can't figure out how to fix this. It doesn't look like I'm calling more elements than are in the array. Here's the code:</p> <pre><code>import acm.program.*; import acm.graphics.*; import java.awt.Color; import java.awt.Font; import javax.swing.*; import java.awt.event.*; public class Simon extends Program implements ActionListener { private int array[]; private int currentSeqLength; private int usersInput; public Simon() { //Initialize Class Values array = new int[20]; currentSeqLength = 1; usersInput = 0; generateSequence(); while(currentSeqLength &lt;= array.length) { playSequence(); //Wait For User's Input Here, Assign To Variable //usersInput = if (pushButton(usersInput)== true) { currentSeqLength++; } else { gameOverMessage(); break; //Reset Variables: } } } public void generateSequence() { //Fill Array With Random Numbers for (int i = 0; i &lt;= array.length; i++ ) { array[i] = (int)(Math.random()*4); } } public void setLength(int length) { //Set Current Length To Size Of Given Argument currentSeqLength = length; } int getLength() { return currentSeqLength; } int[] playSequence() { //Print Out The Current Sequence //New Local Array To Return int newArray[]= new int[currentSeqLength]; //Repeat As Many Times As Value Of currentSeqLength for(int i = 0; i &lt;= currentSeqLength ; i++) { System.out.println(array[i]); //Return an array of int's to the player. newArray[i] = array[i]; } return newArray; } boolean pushButton(int usersInput) { //Given A Button Press (0-3), Return Whether That Was The //Correct Button To Play At The Moment if (usersInput == array[currentSeqLength]) { return true; } else { return false; } } boolean isTurnOver() { //If Current Sequence Length Matches Or Exceeds Value Of //Array Element In Location Of Current Sequence Length if (currentSeqLength &gt;= array[currentSeqLength]) { return true; } else { return false; } } //Not Needed? boolean isGameOver() { if (pushButton(usersInput) == false) { return true; } else { return false; } } String gameOverMessage() { return "Game Over"; } /*public void actionPerformed(ActionEvent event) { int input; } */ </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload