I'm doing a program which has to guess the number you're thinking of between 1000-9999 in less than 20 guesses. So far, this is what I've got. It gives me the error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4500, Size: 4500
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at GuessingGame.updateMyGuess(GuessingGame.java:52)
at GuessingGame.main(GuessingGame.java:124)
not sure why!
this is my program:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4500, Size: 4500
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at GuessingGame.updateMyGuess(GuessingGame.java:52)
at GuessingGame.main(GuessingGame.java:124)
not sure why!
this is my program:
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class GuessingGame {
public int total; // keeps track of number of guesses made
public static int myguess = 0;
public ArrayList<String> guesses; // creat the arraylist
public GuessingGame ( ) {
//initialize array with capacity 9000
guesses = new ArrayList<String>(9000);
//fills up array list with numbers 1000 through to 9999
for(int i = 0; i < 9000; i++) {
guesses.add(Integer.toString(1000+i));
}
}
public int myGuessIs() {
//guess is invalid when the array list is empty (i.e. the size is 0)
if(guesses.size()==0){
return -1;
}
//randomly picks a number thats left in the array list
int guess1 = Integer.parseInt(guesses.get((int)(Math.random()*guesses.size())));
total++;
return guess1;
}
public int totalNumGuesses() {
//returns the number of guesses made
return total;
}
public void updateMyGuess(int nmatches) {
//removes the guess itself, and other impossible numbers from the ArrayList
String myGuess2= Integer.toString(myguess);
int matches = 0;
for(int i = 0; i<guesses.size(); i++) {
//for(int j = 0; j<guesses.size(); j++ ){
if(nmatches!=matches||((guesses.get(i)).equals(myGuess2))){
guesses.remove(i);
}
for(int k = 0; k<4; k++){
if(myGuess2.charAt(k)==(guesses.get(i)).charAt(k)){
matches++;
}
}
}
}
public static void main(String[] args) {
GuessingGame gamer = new GuessingGame();
JOptionPane.showMessageDialog(null, "Think of a number between 1000 and 9999.\n Click OK when you are ready...", "Let's play a game", JOptionPane.INFORMATION_MESSAGE);
int numMatches = 0;
do {
myguess = gamer.myGuessIs();
if (myguess == -1) {
JOptionPane.showMessageDialog(null, "I don't think your number exists.\n I could be wrong though...", "Mistake", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
String userInput = JOptionPane.showInputDialog("I guess your number is " + myguess + ". How many digits did I guess correctly?");
// quit if the user input nothing (such as pressed ESC)
if (userInput == null)
System.exit(0);
// parse user input, pop up a warning message if the input is invalid
try {
numMatches = Integer.parseInt(userInput.trim());
}
catch(Exception exception) {
JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE);
continue;
}
// the number of matches must be between 0 and 4
if (numMatches < 0 || numMatches > 4) {
JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE);
continue;
}
if (numMatches == 4)
break;
// update based on user input
gamer.updateMyGuess(numMatches);
} while (true);
// the game ends when the user says all 4 digits are correct
System.out.println("Aha, I got it, your number is " + myguess + ".");
System.out.println("I did it in " + gamer.totalNumGuesses() + " turns.");
}
}