Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2012
    Rep Power

    Default Problem with method call throwing ArrayIndexOutOfBoundsException

    Hi all,

    I am writing a program for school, and am stuck. The program essentially creates a random number, puts that number in an array and then tests user's inputs versus that number and returns feedback according to how close the guess is to the number. I am having a problem in the middle of the program where the computer generates the number and then puts it into the array. When I run the method independently (in a main method by itself) I can generate the array without a problem, but when the method is called from the 'mother' program, it throws an array index out of bounds exception. I realize this usually happens when the array has an index that is less than zero, but I have stared at my code for twenty minutes and don't see any way that that could be the case. Here's the mother program in question. The line that throws the exception is engine.generateNewSecret():

    Java Code:
    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(;
    		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: "); =; // 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.guess, engine.getNumDigits());
    						if (validator.validateGuess(engine.getSecret(),
    								player.guess, engine.getNumDigits()) == false) 
    					System.out.println("Congratulations! You won in "
    							+ numberOfGuesses + "moves!");
    					if (newNumberOfGuesses < numberOfGuesses)
    						player.fastestWin = newNumberOfGuesses;
    						player.fastestWin = numberOfGuesses;
    					System.out.println("\nStatistics for " + + ":");
    					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")
    And here's the Engine class code:
    Java Code:
    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)
    			number = number%((int)(Math.pow(10, temp-1)));
    public byte getNumDigits()
    	return numDigits;
    public byte[] getSecret()
    	return secretNumber;
    	 * @param args
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    I am sure it's something obvious I am missing, but for the life of me I can't figure out what it is. Any and all help would be appreciated!!

    Thanks :)
    Last edited by jameschristian; 11-02-2012 at 06:58 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

    Default Re: Problem with method call throwing ArrayIndexOutOfBoundsException

    I'd start by simplifying your random number generation - what is the purpose of all that Math.pow complexity?

Similar Threads

  1. method call problem
    By dawnMist in forum New To Java
    Replies: 15
    Last Post: 10-04-2012, 03:58 PM
  2. Replies: 3
    Last Post: 03-27-2012, 11:23 PM
  3. Method call problem.
    By Lufc in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 09:00 PM
  4. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 02:34 PM
  5. Replies: 2
    Last Post: 06-02-2008, 10:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts