Hi all,
I am writing a program for school, and am stuck. The program is throwing an ArrayIndexOutOfBounds exception, which I realize means that my array has an index that is less than zero, but I can't see how that's happening. Can you please look over my code and see if it's something obvious? The program generates a random number and then compares a user guess to that number and provides feedback regarding how close the guess is. The problem happens about halfway down at "engine.generateNewSecret()". When I run this method independently, it will create the array without a problem, but inside of the 'mother' program it throws the exception.
Here you go:
import java.util.Scanner; public class Bagel { public static void main(String[] args) { System.out.println("Welcome!"); // it's nice to be nice :) Scanner keyboard = new Scanner(System.in); Engine engine = new Engine(); // declare object instances for this class Player player = new Player(); Validator validator = new Validator(); boolean reset = true; // menu switch while (reset == true) { System.out.println("\nEnter the number of digits to use: "); engine.numDigits = keyboard.nextByte(); // gets the numDigits System.out.println("\nEnter the player's name: "); player.name = keyboard.next(); // gets the player's name boolean play = true; // menu switch while (play == true) { for (int gameCount = 1; gameCount > 0; gameCount++) { System.out.println("\n Starting game " + gameCount); engine.generateNewSecret(); // [B]here's the problem![/B] int numberOfGuesses = 0; int newNumberOfGuesses; while (true) { newNumberOfGuesses = numberOfGuesses; System.out.println("\nEnter guess: "); player.askForGuess(); validator.validateGuess(engine.getSecret(), player.guess, engine.getNumDigits()); if (validator.validateGuess(engine.getSecret(), player.guess, engine.getNumDigits()) == false) { numberOfGuesses++; continue; } else break; } gameCount++; System.out.println("Congratulations! You won in " + numberOfGuesses + "moves!"); if (newNumberOfGuesses < numberOfGuesses) player.fastestWin = newNumberOfGuesses; else player.fastestWin = numberOfGuesses; System.out.println("\nStatistics for " + player.name + ":"); System.out.println("\nGames completed: " + gameCount); System.out.println("\nNumber of digits: " + engine.getNumDigits()); System.out.println("\nFastest win: " + player.fastestWin); } System.out.println("p - Play again\nr - Reset game\nq - Quit"); System.out.println("\nWhat would you like to do?"); String menu = keyboard.nextLine().trim().toLowerCase().substring(0,1); if (menu == "p") play = true; else if (menu == "r") { play = false; reset = true; } else if (menu == "q") { System.out.println("/nGoodbye!"); System.exit(0); } } } } }
Here's the code for the Engine class:
import java.util.Random; public class Engine { Random random = new Random(); byte numDigits; byte[] secretNumber = new byte[numDigits]; public void generateNewSecret() { int number = random.nextInt(((int)Math.pow(10, numDigits) - ((int)Math.pow(10, numDigits-1)))) + ((int)Math.pow(10, numDigits-1)); int temp = numDigits; for (int i = 0; i < numDigits; i++) { secretNumber[i] = (byte)((number/(Math.pow(10, temp-1)))); // [B]Exception here![/B] if (number<10) break; number = number%((int)(Math.pow(10, temp-1))); temp--; } } public byte getNumDigits() { return numDigits; } public byte[] getSecret() { return secretNumber; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }