Results 1 to 8 of 8
  1. #1
    HereIam is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Exclamation Please Help... switch statement not recognising variable..and I can't figure out why

    We have to make a tic tac toe game for my introduction to java class... and I've been working at this for days now... including completely breaking it down and restarting it... no GUI used... anyways.. I got it to where it only has one error left..(that it's telling me anyway) and that error is that it doesn't recognize my variable in my switch statement.. This is what i have so far.. I know I did something wrong.. but i can't figure out what it is.. somebody please please please help.. and thank you tons!!

    Java Code:
      import java.io.*;
    
      public class H2_TicTacToe
      {
    	  public static void main(String args[]) throws IOException
    	  {
    	  	 String welcome = "Welcome to Michelle's Tic-Tac-Toe Game";
    		 String[] board = {"","","","","","","","",""};
    
    		 int markType;
    		 String inputMark;
    		 BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    
    		 for(markType = 0; markType < 9; markType++){
    		 menu();
    
    		 inputMark = dataIn.readLine();
    		 markType = Integer.parseInt(inputMark);
    		 addMark(markType, board);
    
    		 showBoard(board);
    	         }
      	  }
    
     
     public static void addMark(int maType, String theBoard[])
    
     {
    	 switch(maType)
    	 {
    	 case 1:
    	 if(theBoard[0] == "")
    	 	{
    			if(markType % 2 = 0){
    				theBoard[0] = "X";
    				System.out.println("O's Turn to go. Please pick a space");
    				menu();
    			}else{
    				theBoard[0] = "O";
    				System.out.println("X's Turn to go. Please pick a space");
    				menu();
    			}
    		}
    	 else{
    		 System.out.println("Space already claimed. Please pick another space");
    		 menu();
    	    }
         }
     }
    }
    other cases haven't been put in yet..and menu method excluded... this is the error it's giving me:
    Documents\H2_TicTacToe.java:70: error: cannot find symbol
    if(markType % 2 = 0){
    ^
    symbol: variable markType
    location: class H2_TicTacToe
    1 error

    Tool completed with exit code 1

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Please Help... switch statement not recognising variable..and I can't figure out

    Java Code:
    Documents\H2_TicTacToe.java:70: error: cannot find symbol
    			if(markType % 2 = 0){
    			   ^
      symbol:   variable markType
      location: class H2_TicTacToe
    1 error
    The "cannot find symbol" message means that you haven't declared markType anywhere. The two common reasons for this are (1) You have made a typo (spelled something wrong, wrong capitalisation etc) (2) You have called a method with the wrong type of arguments. (method wanted an int, you gave it a String: that sort of thing).

    In this case it's (1). The variable in question is not markType, it is something else.

  3. #3
    HereIam is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Please Help... switch statement not recognising variable..and I can't figure out

    oh no... ok.. thank you so much for responding.. i'll try to figure it out

  4. #4
    HereIam is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Please Help... switch statement not recognising variable..and I can't figure out

    if that was something else.. would I also have to change my for statement in the beginning with that something else?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Please Help... switch statement not recognising variable..and I can't figure out

    No you don't have to change anything in the for loop. All that matters is that you are consistent with what you are calling the mark type within the addMark() method.

    Of course there may be other things wrong that do affect the for loop (I haven't looked). But you can resolve the "cannot find symbol" in addMark() just by changing the variable the compiler is grumbling about to what it should be to be consistent with the rest of the method.

  6. #6
    HereIam is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Please Help... switch statement not recognising variable..and I can't figure out

    feeling super stupid right about now... I went through and changed the variable so that it matched throughout the method.. and then it gave me an error with the modulo sign saying it found a value when it required a variable.. everything I saw was typed exactly the same.. i'm so confused... unless you mean the type.. but i assumed you meant i wrote it wrong.. which i probably did.. but when i tried to fix it like you said.. i must have done something else wrong..

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Please Help... switch statement not recognising variable..and I can't figure out

    If you are getting a new compiler message, post it and the new markType() method.

    (Note that only one variable had to change: the one the original message said was wrong.)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Please Help... switch statement not recognising variable..and I can't figure out

    To break it down a little:
    Java Code:
    addMark(markType, board);
    That is how you are calling addMark.

    The addMark method signature looks like this:
    Java Code:
    public static void addMark(int maType, String theBoard[])
    So the variable markType is being passed into addMark.
    According to the signature that value is stored in a variable called maType.

Similar Threads

  1. I cant figure out why this variable isn't recognized...
    By nksjolinder in forum New To Java
    Replies: 6
    Last Post: 09-26-2011, 04:59 PM
  2. switch statement
    By droidus in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 09:54 AM
  3. Replies: 6
    Last Post: 03-31-2011, 03:33 PM
  4. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 02:22 PM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM

Posting Permissions

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