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

    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(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);
    
    					
    				}
    	
    					
    			}
    
    		}
    
    	}
    
    }
    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)
    		    	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
    
    	}
    
    }
    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 07:58 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    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, 04:58 PM
  2. Replies: 3
    Last Post: 03-28-2012, 12:23 AM
  3. Method call problem.
    By Lufc in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 10:00 PM
  4. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 03:34 PM
  5. Replies: 2
    Last Post: 06-02-2008, 11: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
  •