Results 1 to 8 of 8
- 02-13-2012, 11:45 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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!!
other cases haven't been put in yet..and menu method excluded... this is the error it's giving me: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(); } } } }
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
- 02-14-2012, 12:07 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: Please Help... switch statement not recognising variable..and I can't figure out
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).Java Code:Documents\H2_TicTacToe.java:70: error: cannot find symbol if(markType % 2 = 0){ ^ symbol: variable markType location: class H2_TicTacToe 1 error
In this case it's (1). The variable in question is not markType, it is something else.
- 02-14-2012, 12:11 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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
- 02-14-2012, 12:14 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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?
- 02-14-2012, 12:40 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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.
- 02-14-2012, 12:52 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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..
- 02-14-2012, 03:08 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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.)
- 02-14-2012, 10:12 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Please Help... switch statement not recognising variable..and I can't figure out
To break it down a little:
That is how you are calling addMark.Java Code:addMark(markType, board);
The addMark method signature looks like this:
So the variable markType is being passed into addMark.Java Code:public static void addMark(int maType, String theBoard[])
According to the signature that value is stored in a variable called maType.
Similar Threads
-
I cant figure out why this variable isn't recognized...
By nksjolinder in forum New To JavaReplies: 6Last Post: 09-26-2011, 04:59 PM -
switch statement
By droidus in forum New To JavaReplies: 2Last Post: 09-21-2011, 09:54 AM -
Can't figure out why my Else statement won't work
By basla in forum New To JavaReplies: 6Last Post: 03-31-2011, 03:33 PM -
help with switch statement
By java__beginner in forum New To JavaReplies: 4Last Post: 03-19-2009, 02:22 PM -
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02-06-2008, 05:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks