Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default Objects and Classes cannot find symbol variable.

    Here's the code:
    Java Code:
    public class BoardClass
    {
    	public char[][]board;
    	public int row;
    	public int column;
    	public int direction;
    	public boolean solved;
    	
    	public BoardClass()
    	{
    		board = new char[5][5];
    		row=0;
    		column=0;
    		direction=0;
    		solved=false;
    		makeBoard();
    		LegalMove();
    		makeMove();	
    		unmakeMove();
    		golfPeg(board, solved);		
    	}
    	public char[][] makeBoard()
    	{
    		for(row=0; row < 5; row++)
    		{
    			for(column=0; column <= row; column++)
    			{
    				board[row][column] = '*';
    				board[0][0] = '.';
    				System.out.print(board[row][column] + " ");
    			}
    			
    			for(column = row + 1; column < board.length; column++)
    			{
    				board[row][column] = '/'; 
    				System.out.print(board[row][column] + " ");
    			}
    			System.out.println("");
    				
    		}
    		return(board);
    	}
    	public boolean LegalMove()
    	{
    		switch(direction)
    		{
    			case 1: //NORTH
    				if(board[row-1][column] == '*' && board[row-2][column]== '.')
    				{
    					return(true);
    				}
    				break;
    				
    			case 2: //EAST
    				if(board[row][column+1] == '*' && board[row][column+2] == '.')
    				{
    					return(true);
    				}
    				break;
    				
    			case 3://SOUTHEAST
    				if(board[row+1][column+1] == '*' && board[row+2][column+2] == '.')
    				{
    					return(true);
    				}
    				break;
    				
    			case 4://SOUTH
    				if(board[row+1][column] == '*' && board[row+2][column] == '.')
    				{
    					return(true);
    				}
    				break;
    			
    			case 5://WEST
    				if(board[row][column-1] == '*' && board[row][column-2] == '.')
    				{
    					return(true);
    				}
    				break;
    				
    			case 6://NORTHWEST
    				if(board[row-1][column-1] == '*' && board[row-2][column-2] == '.')
    				{
    					return(true);
    				}
    				break;
    		}
    		return(false);
    	}
    	public boolean Done()
    	{
    		boolean bool=false;
    		int pegAmount=0;
    		for(int x=0; x<=5; x++)
    		{
    			for(int y=0; y<=x+1; y++)
    			{
    				if(board[x][y] == '*')
    				{
    					pegAmount++;
    					if(pegAmount > 1)
    					{
    						bool = false;
    					}
    					if(pegAmount==1)
    					{
    						bool = true;
    					}
    				}
    			}
    		}
    		return(bool);
    	}
    	public void makeMove()
    	{
    		switch(direction)
    		{
    			case 1: //NORTH
    				board[row-2][column]=board[row][column];
    				board[row-1][column] = '.';
    				board[row][column] = '.';				
    				break;
    				
    			case 2: //EAST
    				board[row][column+2]=board[row][column];
    				board[row][column+1]= '.';
    				board[row][column]='.';
    				break;
    				
    			case 3://SOUTHEAST
    				board[row+2][column+2]=board[row][column];
    				board[row+1][column+1] = '.';
    				board[row][column] = '.';
    				break;
    				
    			case 4://SOUTH
    				board[row+2][column]=board[row][column];
    				board[row+1][column] = '.';
    				board[row][column] = '.';
    				break;
    			
    			case 5://WEST
    				board[row][column-2]=board[row][column];
    				board[row][column-1] = '.';
    				board[row][column] = '.';
    				break;
    				
    			case 6://NORTHWEST
    				board[row-2][column-2]=board[row][column];
    				board[row-1][column-1]= '.';
    				board[row][column] = '.';
    				break;
    		}
    		
    	}
    	public void unmakeMove()
    	{
    		switch(direction)
    		{
    			case 1: //NORTH unmake
    				board[row+2][column]=board[row][column];
    				board[row+1][column] = '*';
    				board[row][column] = '*';				
    				break;
    				
    			case 2: //EAST unmake
    				board[row][column-2]=board[row][column];
    				board[row][column-1]= '*';
    				board[row][column]='*';
    				break;
    				
    			case 3://SOUTHEAST unmake
    				board[row-2][column-2]=board[row][column];
    				board[row-1][column-1] = '*';
    				board[row][column] = '*';
    				break;
    				
    			case 4://SOUTH unmake
    				board[row-2][column]=board[row][column];
    				board[row-1][column] = '*';
    				board[row][column] = '*';
    				break;
    			
    			case 5://WEST unmake
    				board[row][column+2]=board[row][column];
    				board[row][column+1] = '*';
    				board[row][column] = '*';
    				break;
    				
    			case 6://NORTHWEST unmake
    				board[row+2][column+2]=board[row][column];
    				board[row+1][column+1]= '*';
    				board[row][column] = '*';
    				break;
    		}
    	}
    	public void golfPeg(char board[][], boolean solved)
    	{
    		if(Done() == true)
    		{
    			solved = true;
    		}
    		else
    		{
    			row = 0;
    			do
    			{
    				row = row + 1;
    				column = 0;
    				do
    				{
    					column = column + 1;
    					direction = 0;
    					do
    					{
    						direction = direction + 1;
    						if(LegalMove() == true)
    						{
    							makeMove();
    							golfPeg(board, solved);
    							if(solved == false)
    							{
    								unmakeMove();
    							}
    						}
    					}
    					while(!solved || direction == 6);
    				}
    				while(!solved || column == row);
    			}
    			while(!solved || row == 5);
    		}
    	}
    }
    |||||||||||||||||||||||||||||OTHER FILE|||||||||||||||||||||||||||||

    Java Code:
    public class Tester
    {
    	
    	public static void main(String args[])
    	{
    		BoardClass board1 = new BoardClass();
    		solved = false;
    		board1.golfPeg(board, solved);
    		if(solved = true)
    		{
    			board1.makeBoard();
    			do
    			{
    				board1.makeMove();
    				board1.makeBoard();
    			}
    			while(!solved);
    		}
    		else
    		{
    			System.out.println("There is no solution for the board shown.");
    			board1.makeBoard();
    		}
    	}
    }
    //I know I am not done with this code but I am getting errors saying that the symbol variables board and solved are not found although i thought that making the object called those from the other file.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    What lines are the errors on?
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    they are on... the second file on all the lines containing solved and/or board

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    In the class Tester variable solved appears to have no type

  5. #5
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    ok, but I thought that the creation of the object took the variable from the constructor and used the same one.

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    For "solved" you aren't declaring that variable anywhere. It looks like it is supposed to be boolean type, but you just try to assign a false value to something you haven't declared in main.

    After you fix that, tell me what errors you still have left.

    EDIT: Awe, Marshy beat me to it :)
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    or is that not how it works?

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by argnsoccer View Post
    ok, but I thought that the creation of the object took the variable from the constructor and used the same one.

    You'd need to write it like this:

    Java Code:
    board1.solved = false;

    But it already looks like you set it to false in the constructor...
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    Is there a way I can use the same solved from the other file?

  10. #10
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    oh ok!! Thanks! and I can just do the same for board then. Thank you I will come back if i find any problems after that!

    Thanks again,
    argnsoccer

  11. #11
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    ok well now my program compiles well, but I get a runtime out of bounds error. The errors are shown at "if(Done() == true)" in the golfPeg method, " if(board[x][y] == '*')" in the Done method, "golfPeg(board, solved);" in the constructor, and the creation of the object (BoardClass board1 = new BoardClass(); ) in the main

  12. #12
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    ok well I'm fixing everything, I will come back when I have any further questions. for now, I can work on my own fixing bounds errors and such.

  13. #13
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Could be because you are using the same variable names for the parameters of those methods as you have already established to be global variables.
    Sincerely, Joshua Green
    Please REP if I help :)

  14. #14
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    Possibly... Although now I am getting bounds errors at "if(LegalMove() == true)" in the golfPeg method, and "if(board[row-1][column] == '*' && board[row-2][column]== '.')" in case 1 of the LegalMove switch

  15. #15
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Could you post an updated code for us?
    Sincerely, Joshua Green
    Please REP if I help :)

  16. #16
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default Updated Code

    yes, sure.
    Java Code:
    public class BoardClass
    {
    	public char[][]board;
    	public int row;
    	public int column;
    	public int direction;
    	public boolean solved;
    	
    	public BoardClass()
    	{
    		board = new char[5][5];
    		row=0;
    		column=0;
    		direction=0;
    		solved=false;
    		makeBoard();
    		LegalMove(board);
    		makeMove();	
    		unmakeMove();
    		[COLOR="red"]golfPeg(board, solved);	[/COLOR]	
    	}
    	public char[][] makeBoard()
    	{
    		for(row=0; row < 5; row++)
    		{			
    			for(column=0; column <= row; column++)
    			{
    				board[row][column] = '*';
    				board[0][0] = '.';
    				System.out.print(board[row][column] + " ");
    			}
    			for(column = row + 1; column < board.length; column++)
    			{
    				board[row][column] = '/'; 
    				System.out.print(board[row][column] + " ");
    			}
    			System.out.println("");
    				
    		}
    		return(board);
    	}
    	public boolean LegalMove(char board[][])
    	{
    		switch(direction)
    		{
    			case 1: //NORTH
    				if(board[row-1][column] == '*' && board[row-2][column]== '.')
    				{
    					return(true);
    				}
    				break;
    				
    			case 2: //EAST
    				if(board[row][column+1] == '*' && board[row][column+2] == '.')
    				{
    					return(true);
    				}
    				break;
    				
    			case 3: //SOUTHEAST
    				if(board[row+1][column+1] == '*' && board[row+2][column+2] == '.')
    				{
    					return(true);
    				}
    				break;
    				
    			case 4: //SOUTH
    				if(board[row+1][column] == '*' && board[row+2][column] == '.')
    				{
    					return(true);
    				}
    				break;
    			
    			case 5: //WEST
    				[COLOR="Red"]if(board[row][column-1] == '*' && board[row][column-2] == '.')[/COLOR]
    				{
    					return(true);
    				}
    				break;
    				
    			case 6: //NORTHWEST
    				if(board[row-1][column-1] == '*' && board[row-2][column-2] == '.')
    				{
    					return(true);
    				}
    				break;
    		}
    		return(false);
    	}
    	public boolean Done()
    	{
    		boolean bool=false;
    		int pegAmount=0;
    		for(int x=0; x < 5; x++)
    		{
    			for(int y=0; y < x+1; y++)
    			{
    				if(board[x][y] == '*')
    				{
    					pegAmount++;
    					if(pegAmount > 1)
    					{
    						bool = false;
    					}
    					if(pegAmount==1)
    					{
    						bool = true;
    					}
    				}
    			}
    		}
    		return(bool);
    	}
    	public void makeMove()
    	{
    		switch(direction)
    		{
    			case 1: //NORTH
    				board[row-2][column]=board[row][column];
    				board[row-1][column] = '.';
    				board[row][column] = '.';				
    				break;
    				
    			case 2: //EAST
    				board[row][column+2]=board[row][column];
    				board[row][column+1]= '.';
    				board[row][column]='.';
    				break;
    				
    			case 3://SOUTHEAST
    				board[row+2][column+2]=board[row][column];
    				board[row+1][column+1] = '.';
    				board[row][column] = '.';
    				break;
    				
    			case 4://SOUTH
    				board[row+2][column]=board[row][column];
    				board[row+1][column] = '.';
    				board[row][column] = '.';
    				break;
    			
    			case 5://WEST
    				board[row][column-2]=board[row][column];
    				board[row][column-1] = '.';
    				board[row][column] = '.';
    				break;
    				
    			case 6://NORTHWEST
    				board[row-2][column-2]=board[row][column];
    				board[row-1][column-1]= '.';
    				board[row][column] = '.';
    				break;
    		}
    		
    	}
    	public void unmakeMove()
    	{
    		switch(direction)
    		{
    			case 1: //NORTH unmake
    				board[row+2][column]=board[row][column];
    				board[row+1][column] = '*';
    				board[row][column] = '*';				
    				break;
    				
    			case 2: //EAST unmake
    				board[row][column-2]=board[row][column];
    				board[row][column-1]= '*';
    				board[row][column]='*';
    				break;
    				
    			case 3://SOUTHEAST unmake
    				board[row-2][column-2]=board[row][column];
    				board[row-1][column-1] = '*';
    				board[row][column] = '*';
    				break;
    				
    			case 4://SOUTH unmake
    				board[row-2][column]=board[row][column];
    				board[row-1][column] = '*';
    				board[row][column] = '*';
    				break;
    			
    			case 5://WEST unmake
    				board[row][column+2]=board[row][column];
    				board[row][column+1] = '*';
    				board[row][column] = '*';
    				break;
    				
    			case 6://NORTHWEST unmake
    				board[row+2][column+2]=board[row][column];
    				board[row+1][column+1]= '*';
    				board[row][column] = '*';
    				break;
    		}
    	}
    	public void golfPeg(char board[][], boolean solved)
    	{
    		if(Done() == true)
    		{
    			solved = true;
    		}
    		else
    		{
    			row = 0;
    			do
    			{
    				row = row + 1;
    				column = 0;
    				do
    				{
    					column = column + 1;
    					direction = 0;
    					do
    					{
    						direction = direction + 1;
    						[COLOR="red"]if(LegalMove(board) == true)[/COLOR]
    						{
    							makeMove();
    							golfPeg(board, solved);
    							if(!solved)
    							{
    								unmakeMove();
    							}
    						}
    					}
    					while(!solved || direction == 6);
    				}
    				while(!solved || column == row);
    			}
    			while(!solved || row == 5);
    		}
    	}
    }
    |||||||||||||||MAIN FILE|||||||||||||||||||||
    Java Code:
    import java.util.Scanner;
    
    public class Tester
    {
    	
    	public static void main(String args[])
    	{
    		[COLOR="red"]BoardClass board1 = new BoardClass();[/COLOR]
    		board1.golfPeg(board1.board, board1.solved);
    		if(board1.solved = true)
    		{
    			board1.makeBoard();
    			do
    			{
    				board1.makeMove();
    				board1.makeBoard();
    			}
    			while(!board1.solved);
    		}
    		else
    		{
    			System.out.println("There is no solution for the board shown.");
    			board1.makeBoard();
    		}
    	}
    }
    red is error code

    error/output:

    . / / / /
    * * / / /
    * * * / /
    * * * * /
    * * * * *
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at BoardClass.LegalMove(BoardClass.java:75)
    at BoardClass.golfPeg(BoardClass.java:217)
    at BoardClass.<init>(BoardClass.java:20)
    at Tester.main(Tester.java:8)

  17. #17
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    You can check which array index is causing the exception by using System.out.println() to show the values of row and column just before the line that causes the exception.

    I haven't read the code, but do you really want these variables (and maybe others) to be instance variables of the class? If you use them as "global" variables in this way you have to be certain that they never have values that cause an exception with expressions like "board[row][column-1]".

  18. #18
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    oh I see... that makes sense... I think I made them instance variables at the beginning before doing the whole code and they might be causing problems I guess. I'll do some print lines and see where the error is, thanks!

  19. #19
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    well I inserted a print line into the fifth case and the print line was not reached.

  20. #20
    argnsoccer is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    it doesn't reach any of the cases for that matter, but it does go into the actual method and prints out six times, meaning it is going into the method but not into the switch

Page 1 of 2 12 LastLast

Similar Threads

  1. Java cannot find symbol- variable img
    By crutchfieldj in forum New To Java
    Replies: 3
    Last Post: 04-13-2010, 11:47 PM
  2. Cannot find symbol variable - Why? I can.. ^^
    By Mattedatten in forum New To Java
    Replies: 4
    Last Post: 03-08-2010, 08:07 PM
  3. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 10:51 PM
  4. Cannot find symbol variable pD! I cant fix it!!!
    By Addez in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 09:32 PM
  5. Cannot find symbol variable yourScore
    By Addez in forum New To Java
    Replies: 4
    Last Post: 08-17-2009, 11:48 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
  •