Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Unexpected ArrayOutOfBoundsError

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Unexpected ArrayOutOfBoundsError

    In a program I am writing for homework, one method yields an unexpected error:
    (The variable n has been declared 9 earlier in the program)

    private boolean[] getAllowedValues(int row, int col) {
    		boolean[] valNums = new boolean[n];
     
    		// Set the initial value of all squares to true
    		for (int i = 0; i < n; i++)
    			valNums[i] = true;
     
    		// Check row
    		for (int i = 0; i < n; i++) {
    			if (board[row][i] != LOCATION_EMPTY) 
    				valNums[board[row][i]] = false;
    		}
     
    		// Check column
    		for (int i = 0; i < n; i++) {
    			if (board[i][col] != LOCATION_EMPTY)
    				valNums[board[i][col]] = false;
    		}
     
    		// Check the nine sub-grids
    		int rl = (row / 3) * 3;
    		int rh = (row / 3) * 3 + 3;
    		int cl = (col / 3) * 3;
    		int ch = (col / 3) * 3 + 3;
     
    		for (int r = rl; r < rh; r++) {
    			for (int c = cl; c < ch; c++) {
    				if (board[r][c] != LOCATION_EMPTY) 
    					valNums[board[r][c]] = false;
    			}
    		}
    			System.out.println(valNums);
    		return valNums;		
    	}

    This is the error I get:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
    at Sudoku.SudokuPuzzle.getAllowedValues(SudokuPuzzle. java:232)
    at Sudoku.SudokuPuzzle.main(SudokuPuzzle.java:278)

    This method is called in the main method as a part of the Sudoku constructor as part of a switch:

    case 'H':
    				int row = 0;
    				int col = 0;
    				Scanner in = new Scanner(System.in);		
    				System.out.print("Enter a row for allowed values: ");
    				row = in.nextInt();
    				System.out.print("Enter a column for allowed values: ");
    				col = in.nextInt();
    				Sudoku.getAllowedValues(row, col);

    Thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Unexpected ArrayOutOfBoundsError

    There is some missing information here that makes it hard to diagnose. On which line in particular is the exception thrown (the line in the stack trace and the code you posted do not line up)? You reference an array named 'board'...what is this, and where is this defined? What values are you inputting from the command line?

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Unexpected ArrayOutOfBoundsError

    I'm surprised it's not saying

     


    cannot find symbol "n"





    valNums[board[i][col]] = false;

    This might be the problem.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Unexpected ArrayOutOfBoundsError

    I think I may have a theory as to what may be going wrong.

    Is it possible that you're telling it to do something with index 9, when there'd only be 8 values or something like that?

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unexpected ArrayOutOfBoundsError

    private boolean[] getAllowedValues(int row, int col) {
            boolean[] valNums = new boolean[n];
     
            // Set the initial value of all squares to true
            for (int i = 0; i < n; i++)
                valNums[i] = true;

    Yeah the loop asks for an array index that isn't there. Lets say n = 3. i goes from 0-3 which is 4 indexes. Java spits out error when i = 3 and valNums[] has no index for it.

Similar Threads

  1. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM