2

Okay so.. i have a method that basically checks if the player can buy the field and if he can then sets him as the owner and return true. the method i am referring to is buyFieldFromBank in the if statement.

But i want to check if my method returns true, and if it do, ask the user if he wants to buy it. can i actually do it like this? or will the code allready have set him as owner in the if statement? Or does it only "check" if it turns true, without actually executing the code?

if(landRegistry.buyFieldFromBank(currentPlayer, newPosID)==true){
                    if(GUI.getUserButtonPressed(currentPlayer.getName() + ", vil du købe " + newPosition.getFieldName() +  "Ja", "Nej")=="Ja"){
                        landRegistry.buyFieldFromBank(currentPlayer, newPosID);
  • 3
    It has to execute the code to actually know if it returns true. Also, for that second if-statement, note that you almost certainly do not actually want to compare strings using `==`. See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Dennis Meng Jan 17 '14 at 20:55
  • 1
    You're calling the `landRegistry.buyFieldFromBank` method twice. – Elliott Frisch Jan 17 '14 at 20:56
  • Thanks a lot. That is exactly what i was worried about. :) – SryImStillNewb Jan 17 '14 at 20:58
  • Comparisons to boolean values (`==true`) are redundant. You already have a boolean value; you don't need to use `==` to create a boolean. – jpmc26 Jan 17 '14 at 20:58

2 Answers2

3

Couple of obvious issues:

1) You're using == instead .equals to compare Strings in the 2nd if, well tread debate covering that issue here: Link

2) By calling landRegistry.buyFieldFromBank you are executing that method, so it will already be done. You need a method like landRegistry.isUserEligible(currentPlayer,newPosId) to check in that first if statement. Then if it passes both the first & secondary conditions it should call the buyFieldFromBank method. Also it may be worth considering if that 2nd if should belong in the isUserEligible method call; if it's part of the logic of determining eligibility it might fit best there.

3) The == true part of the first if is redundant if the method you're calling returns a boolean. So you can drop that part and just have the method call itself.

Community
  • 1
  • 1
Durandal
  • 5,575
  • 5
  • 35
  • 49
  • The == is just a brainfart.. i did know that :D.... But thanks! with practice i wont make these dumb mistakes (hopefully :D) And with regards to the method in the if statement, i was unsure, so it is nice to know i cannot do it like that. Good idea with the method that checks if player is elligible. – SryImStillNewb Jan 17 '14 at 21:07
  • @SryImStillNewb Glad to help! Keep at it and always focus on the 'Why` aspect of the answers to these kinds of questions instead of just what works.Practice makes perfect with these kinds of things. – Durandal Jan 17 '14 at 21:11
  • Yea you are definately right. the "Why" aspect is important. I'll keep that in mind! The part about == true being redundant blew me away lol. That i did not know! But i can see it now. Clever! – SryImStillNewb Jan 17 '14 at 21:23
1

If you call a method with . you're executing that method always. How else can it work? You should change your design.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186