Results 1 to 2 of 2
- 11-02-2012, 06:54 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
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():
And here's the Engine class code: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); } } } } }
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!!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 } }
Thanks :)Last edited by jameschristian; 11-02-2012 at 06:58 PM.
- 11-06-2012, 05:18 PM #2
Similar Threads
-
method call problem
By dawnMist in forum New To JavaReplies: 15Last Post: 10-04-2012, 03:58 PM -
ArrayList problem, cant call method from object in array.
By Chip in forum New To JavaReplies: 3Last Post: 03-27-2012, 11:23 PM -
Method call problem.
By Lufc in forum New To JavaReplies: 3Last Post: 05-10-2011, 09:00 PM -
Main method throwing specific Exception
By bugger in forum New To JavaReplies: 5Last Post: 05-13-2009, 02:34 PM -
[SOLVED] Simple Scanner Method - Plus Sign throwing me off...
By Josh.Hoekstra in forum New To JavaReplies: 2Last Post: 06-02-2008, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks