Results 1 to 3 of 3

Thread: Casting problem

  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default Casting problem

    *SSCCE version of the problem*

    I'm trying to cast a SudokuBoard into a SudokuDecipher, but it gives me this:

    Java Code:
    Exception in thread "main" java.lang.ClassCastException: SudokuBoard cannot be cast to SudokuDecipher
    SudokuBoard:

    Java Code:
    public class SudokuBoard {
    	
    	private ArrayList<Integer>[][] initialBoard, board;
    	protected final static int ROW = 9, COLUMN = 9;
    	public final static int EMPTY = 0;
    	
    	public SudokuBoard() {
    		initialBoard = new ArrayList[ROW][COLUMN];
    		board = new ArrayList[ROW][COLUMN];
    		for (int y = 0; y < board.length; y++)
    			for (int x = 0; x < board[y].length; x++) {
    				ArrayList<Integer> cellInitial = new ArrayList<Integer>();
    				ArrayList<Integer> cellFinal = new ArrayList<Integer>();
    				cellInitial.add(0);
    				cellFinal.add(0);
    				board[y][x] = cellFinal;
    				initialBoard[y][x] = cellInitial;
    			}
    	}
    	public SudokuBoard(ArrayList<Integer>[][] initialBoard) {
    		if (initialBoard.length != ROW || initialBoard[0].length != COLUMN)
    			throw new InValidGridException("ROW = " + initialBoard.length + "  COLUMN = " + initialBoard[0].length);
    		this.initialBoard = initialBoard;
    		board = initialBoard;
    	}
           // Methods are not shown.
    SudokuDecipher:
    Java Code:
    public class SudokuDecipher extends SudokuBoard {
    	
    	private boolean boardAltered;
    	private final int REPRESENT_COL = 0,REPRESENT_ROW = 1, REPRESENT_REGION = 2;
    	private int difficultyLevel, attempts;
    
    	//This is the only constructor in this class.
    	public SudokuDecipher(ArrayList<Integer>[][] initialBoard) {
    		super(initialBoard);
    		boardAltered = false;
    		difficultyLevel = 0;
    		attempts = 0;
    	}
            // Methods are not shown.
    Driver:
    Java Code:
    		SudokuBoard board = new SudokuBoard();
    		for (int y = 0; y < 9; y++)
    			for (int x = 0; x < 9; x++) {
    				ArrayList<Integer> cell = new ArrayList<Integer>();
    				cell.add(x+y);
    				board.setCell(x,y,cell);
    			}
    		SudokuDecipher solve = (SudokuDecipher) board;
    1- Why can't I cast?
    Explanation to why I'm doing this: In an actionListener, I'm retrieving info from JTextField[][], each square representing a cell in a sudoku board. I make an instance of SudokuBoard and add each cell to SudokuBoard using sudokuBoard.setCell(col,row,cell). After it's done iterating, I attempt to cast it to a SudokuDecipher and do sudokuBoard.compute() (not a comprehensive explanation). If there is an efficient way of doing this, please let me know, but I still want to know why the casting isn't working.

    2- Looking at the second constructor in SudokuBoard, am I making initialBoard and board aliases? If so, if board changes, initialBoard changes as well right? (not the result I want)


    Thank you very much in advance.
    Last edited by Lil_Aziz1; 05-29-2010 at 08:07 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Java Code:
    		SudokuBoard board = new SudokuBoard();  // [color="red"][b]board is not a SudokuDecipher object![/b][/color]
    		for (int y = 0; y < 9; y++)
    			for (int x = 0; x < 9; x++) {
    				ArrayList<Integer> cell = new ArrayList<Integer>();
    				cell.add(x+y);
    				board.setCell(x,y,cell);
    			}
    		SudokuDecipher solve = (SudokuDecipher) board;
    Why can't I cast?

    Thanks in advance.
    Quite simply because board is not a SudokuDecipher object. The JVM is correct.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Oh sad.. Completely forgot about that. I just anticipated that since it extends SudokuBoard, everything will be fine.

    Thank you! Just got to add a new constructor in SudokuDecipher and it should work :)!

    EDIT: Looking at the second constructor in SudokuBoard, am I making initialBoard and board aliases? If so, if board changes, initialBoard changes as well right? (not the result I want)
    Last edited by Lil_Aziz1; 05-29-2010 at 08:22 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. Casting classX to classX problem
    By no-one in forum Advanced Java
    Replies: 5
    Last Post: 03-17-2011, 05:39 PM
  2. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 08:59 PM
  3. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  4. Casting problem
    By John_28 in forum New To Java
    Replies: 6
    Last Post: 05-14-2008, 12:29 PM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

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