wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Boolean value not changing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean value not changing" Watch "Boolean value not changing" New topic
Author

Boolean value not changing

Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Hi I'm initializing a boolean as false in main, then trying to set it as true in another method, but it won't change to true after calling that second method. I tried creating a separate method so I could return the value of true, but I can't call that method separately through main because of if conditions in the second method. I call the boolean method in the second method, but that's not working either. If anyone can tell me why the boolean's value isn't changing in main, please let me know. Thanks!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
No, the value of a parameter to a method is not changed. Look up "pass by value and pass by reference" which you can find by searching this forum.

By the way, return boo = true; is a bit of dreadful code; it would be nearly as bad with a == instead of the =.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

http://faq.javaranch.com/java/CallByReferenceVsCallByValue


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
That link only confused me more than I already was. So if I can't change the boolean value in the main, what if I create another kind of variable and have that as my while statement, and set that in my CheckWin method?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Please PostRealCode, you're posting a few loose lines and snippets, which makes it harder to see what you're trying to do and what exactly the problem is.

In Java, variables are passed by value. That means that the arguments to a method are new variables, that are initialized with the values of the variables that you pass when you call the method. Changing the value of an argument variable inside the method will not change the original variable in the place where you called the method. Have a look at this:

The variable x in myMethod() is a new variable that is initialized with the value of i (2) when the method is called. Assigning a different value to x inside the method has no effect on variable i in the main method.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Jesper Young wrote:Please PostRealCode, you're posting a few loose lines and snippets, which makes it harder to see what you're trying to do and what exactly the problem is.

In Java, variables are passed by value. That means that the arguments to a method are new variables, that are initialized with the values of the variables that you pass when you call the method. Changing the value of an argument variable inside the method will not change the original variable in the place where you called the method. Have a look at this:

The variable x in myMethod() is a new variable that is initialized with the value of i (2) when the method is called. Assigning a different value to x inside the method has no effect on variable i in the main method.


I don't have the entire code here at work, and didn't want to post a lot of extraneous code.

Okay so I understand that changing it inside a method and calling that method in main won't change the original value, but shouldn't returning a value from another method change the original value? If not, how do I change the original value? If I initialize a new variable in the second method, and have a while statement in main referring to it, it won't recognize that variable.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

you can change the original value if you use the return value to set the variable:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
fred rosenberger wrote:you can change the original value if you use the return value to set the variable:


Can I return through a second method where there are if statements?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
Joe Laviguer wrote: . . . Can I return through a second method where there are if statements?
Don't understand the question.

See if you can make anything out of this thread. It takes a long time to read.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
I can see how returning a value to main would change the original value, but can I return through a secondary method because the secondary as if statements? I don't want to change the value unless the conditions are met. Forgive me if the exact code isn't right...I'm winging it.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

If i understand what you're asking, no.

You're 'myBool' on line 15 will change, but then that gets lost when you return back to your main() method.

You MAY be able to get around this by changing from boolean primitives to Boolean objects/references. I don't recall if they are immutable or not, and can't test right now.

or you could do this:


Some people prefer only one 'return' statement in a method, so it could be adapted to:

Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
fred rosenberger wrote:If i understand what you're asking, no.

You're 'myBool' on line 15 will change, but then that gets lost when you return back to your main() method.

You MAY be able to get around this by changing from boolean primitives to Boolean objects/references. I don't recall if they are immutable or not, and can't test right now.

or you could do this:


Some people prefer only one 'return' statement in a method, so it could be adapted to:



My problem is that the secondary method has a String array in the if statements, so it won't let me return the boolean value to the main because its not a boolean-only method. So now I'm trying to think if there's anything else I can base a while statement off of in the main to end the game when it meets the conditions in the CheckWin method. Or should I just dump the second method and have the if statements in the main method? Thanks for your help.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16


This doesn't make sense. You can't overlap a for and an if like this. You have to completely nest them. Where you're comment says "end for", you're actually closing your 'if' and vice versa.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
fred rosenberger wrote:
This doesn't make sense. You can't overlap a for and an if like this. You have to completely nest them. Where you're comment says "end for", you're actually closing your 'if' and vice versa.


Yes you're right I flip-flopped the end for and end if comments.

But the for and if statements actually run correctly.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

can't you just change the signature so it returns a Boolean?
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
fred rosenberger wrote:can't you just change the signature so it returns a Boolean?


Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method? I also tried a separate boolean only method.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?


You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?


You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?


That's why I'm here in Beginning Java. I don't know these things yet.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?


You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?


That's why I'm here in Beginning Java. I don't know these things yet.


Well, okay. But you're just making things harder for yourself if you start imagining weird stuff like that. If you decide you want your method to return a boolean, just try it. Don't go looking for trouble by trying to dream up reasons why you might not be able to do that, just do it. The trouble will find you soon enough if it actually exists.

And believe it or not, the Java language was designed to be simple. As such, you'll generally find the rules are simple. So for a method, it can be a void method (doesn't return anything) or it can return some type. That's all. It's simple, and there's no reason for the rule to be more complicated than that.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Paul Clapham wrote:
Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?


You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?


That's why I'm here in Beginning Java. I don't know these things yet.


Well, okay. But you're just making things harder for yourself if you start imagining weird stuff like that. If you decide you want your method to return a boolean, just try it. Don't go looking for trouble by trying to dream up reasons why you might not be able to do that, just do it. The trouble will find you soon enough if it actually exists.

And believe it or not, the Java language was designed to be simple. As such, you'll generally find the rules are simple. So for a method, it can be a void method (doesn't return anything) or it can return some type. That's all. It's simple, and there's no reason for the rule to be more complicated than that.


So are you saying the following code would work (I can't test it here at work...have to wait until I get home)? Note: I have boolean bWin = false; initialized in Main.

Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?


You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?


That's why I'm here in Beginning Java. I don't know these things yet.


Well, okay. But you're just making things harder for yourself if you start imagining weird stuff like that. If you decide you want your method to return a boolean, just try it. Don't go looking for trouble by trying to dream up reasons why you might not be able to do that, just do it. The trouble will find you soon enough if it actually exists.

And believe it or not, the Java language was designed to be simple. As such, you'll generally find the rules are simple. So for a method, it can be a void method (doesn't return anything) or it can return some type. That's all. It's simple, and there's no reason for the rule to be more complicated than that.


So are you saying the following code would work (I can't test it here at work...have to wait until I get home)? Note: I have boolean bWin = false; initialized in Main.




I ran that code and java gave me an error that says it needed a return statement before the }//end CheckWin line, but the program won't run correctly if I have the return statement(s) outside the if loops.

Another thing I tried was to completely get away from the CheckWin method and imbed that code into the main method, but that means having to put those if statements in there after the user's turn and after the computer's turn. So I change the boolean value in the main and don't use the CheckWin or Game methods at all, and that seemed to run correctly and end the program after a win or loss, but it won't stop if the user wins. What it does is acknowledge that the user won, then the computer goes ahead and makes the next move anyway, and then it runs through the ifs again and acknowledges the user's victory once again (and if the computer won on its subsequent turn, it actually displays "You win!" and then "You lose!" I'd like it to end the program after the user wins.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

Java requires you to have a return for every possible execution path. I don't think the compiler is smart enough to know that you will ALWAY run in your for loop, so it needs to have something to return just in case the loop is skipped.

Why won't the code run correctly with the return outside the loop? The way it's written now, you will ALWAYS return on the first iteration through the loop:


Another thing I tried was to completely get away from the CheckWin method and imbed that code into the main method,
This is a bad idea. method (including main) should be kept as small as possible. Most people would tell you it's better to pull stuff OUT of a big method to make several smaller ones, rather than vice versa.

((sBoard[0][i] == "X") && (sBoard[1][i] == "X") && (sBoard[2][i] == "X"))
|| ((sBoard[i][0] == "X") && (sBoard[i][1] == "X") && (sBoard[i][2] == "X"))
|| ((sBoard[0][0] == "X") && (sBoard[1][1] == "X") && (sBoard[2][2] == "X"))
|| ((sBoard[0][2] == "X") && (sBoard{1][1] == "X") && (sBoard[2][0] == "X"))

This is REALLY ugly. it's hard to read. I'd probably pull this into a method by itself...something like



Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
fred rosenberger wrote:Java requires you to have a return for every possible execution path. I don't think the compiler is smart enough to know that you will ALWAY run in your for loop, so it needs to have something to return just in case the loop is skipped.

Why won't the code run correctly with the return outside the loop? The way it's written now, you will ALWAYS return on the first iteration through the loop:


REPLY: Well that's the thing. I only want the boolean changed to true when the if conditions are met. If the user has three X's in a row, he wins, and the game is over. If changing the boolean to true is outside of the if loop, it will change the boolean no matter what happens, so theoretically, the game will be over after the very first move. Is my logic correct?

Another thing I tried was to completely get away from the CheckWin method and imbed that code into the main method,
This is a bad idea. method (including main) should be kept as small as possible. Most people would tell you it's better to pull stuff OUT of a big method to make several smaller ones, rather than vice versa.

((sBoard[0][i] == "X") && (sBoard[1][i] == "X") && (sBoard[2][i] == "X"))
|| ((sBoard[i][0] == "X") && (sBoard[i][1] == "X") && (sBoard[i][2] == "X"))
|| ((sBoard[0][0] == "X") && (sBoard[1][1] == "X") && (sBoard[2][2] == "X"))
|| ((sBoard[0][2] == "X") && (sBoard{1][1] == "X") && (sBoard[2][0] == "X"))

This is REALLY ugly. it's hard to read. I'd probably pull this into a method by itself...something like





REPLY: I'll have to write different variations of the code, first without any subsequent methods, make that work, then start breaking the different pieces out into their own methods. If only I had a way to test this code at work...no java IDE or JDK on these systems, USB devices are not authorized on the network, and they won't let me bring in my own network. UGH!

Thanks for your help....its much appreciated!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

Friendly tip...you don't need to quote an entire post when you reply.

I would really suggest you not do it that way - writing one big method that does everything, then breaking it down to smaller ones. You're setting yourself up for failure.

The accepted best way is to write teeny tiny pieces at a time. Get one little thing to work, then add the next smallest thing you can. A lot of the time, you will write 'dummy' method bodies. for example, the first time you write a "winnerFoundAcross()" method, the entire body might be nothing more than this:



you can write this, then test the rest of your code. When you need to, you can force it to return FALSE to test stuff - and that's easy to change. Eventually, you get to the point where you need actually have code here. You will be confident that the rest of your code is solid, since you've been testing it all along.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
I've been thinking about the return statement some more, and my thinking is that I can change the boolean from false to true inside the if statement(s), then return the boolean at the end of the boolean method. I'll just post the CheckWin method code; please let me know if you think this will work. Also, the final if statement...should that be an else if statement? Thanks!

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

what IF-block do you think it should be ELSE-IFed with?

more notes...

a method should do one thing, and one thing only.

Your method:

a) checks to see if X wins
b) prints out a message if he does
c) checks to see if O wins
d) prints a message if he does
e) prints a message if the game is a tie

and finally, it returns TRUE if there is a tie -which seems wrong to me based on the name "CheckWin". a tie is not a win.

I would strongly suggest you perhaps do something like

checkWin() calls a checkXWin() method and a checkOWin() method, sort of like


Then, in your main loop, after each move, call the checkWin. If it's true, you know the game is over. If false, then check to see if the number of moves is 9. If not, loop back around for the next move. If so, you can print your Tie method.

of course, now that I write that, I see a problem - you can't tell who won. maybe in your main loop you need to call checkXWin directly, handle a win appropriately, and if not, call checkOWin, etc....

Am I confusing you?
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Nope you're not confusing me. You're just highlighting more possibilities than I thought of and I really appreciate it.

The IF that I thought about changing to ELSE-IF was the last one, if the iCount = 9 then its a tie. I don't want a winner at the very last turn being told he won and its a tie, so if X isn't a winner (first IF), and O isn't a winner (second IF), then its a tie only if there were 9 turns, not if nobody has won yet if its less than 9.

But I see your point about multiple CheckWin methods. I can call the method that checks to see if X won at the end of X's turn, then call the method to see if O won after O's turn. But I think that's what you're saying. Not sure how to create a separate method to test for a tie without having to run through the IF scenarios all over again, so I'll probably just put the test for a tie in both the X and O methods.

Thanks very much!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

I don't think you need a method to CHECK for a tie. If X didn't win, and O didn't win, and there have been 9 moves...it HAS to be a tie. So...

someone makes a move...
check if X wins
if so, game over X WINS!!!
check if Y wins
if so, game over O WINS!!!
check if total moves == 9
if so, game over TIE!!!
nobody won, so go back to top of this loop.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
It all seems so simple now!



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean value not changing
 
Similar Threads
TicTacToe Game..
Simple: Turning A Program Into An Applet
Help with a chess program
Refering to another class's instance of a different class, inside a class
How would i break this down into seperate metods?