0

I am trying to determine upon startup whether the in-app has been purchased, this is my current code;

if (!(getSharedPreferences("purchased", 0).getBoolean("purchased", false)))
admob();        
Log.d("NOT UPGRADED", "SHOW AD AS UPGRADED ALREADY");
if (!(getSharedPreferences("purchased", 0).getBoolean("purchased", true)))
admobskip();    
Log.d("UPGRADED", "DO NOT SHOW AD AS UPGRADED ALREADY");

But it appears to run through all that code, rather than checking, with iOS I use if / else , however I seem unable to use that and wanted the java equivelent for this piece of code if possible? I have swapped the purchased to 'true' on the second half of statement.

4
  • 4
    Less idiosyncratic indentation would certainly help us, and probably, in time, you as well. Commented Sep 22, 2016 at 11:38
  • Note that your Log.d statements are not part of the if statements, so they are always executed. Commented Sep 22, 2016 at 11:38
  • 1
    Always use braces and you will figure out what the issue is Commented Sep 22, 2016 at 11:38
  • 1
    use { ... } scope Commented Sep 22, 2016 at 11:43

2 Answers 2

3

If you only want statements to run if an if condition passes, then use scope blocks { and } around that code:

if (condition){
    /*run all these statements*/
}

If you don't use the braces then only the following single statement is part of the if: that's what's happening in your case.

(Personally I always use braces as I believe that's a clearer style, but not everyone does that).

Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful, thank you very much, explained well also!
1

Use this instead

if (getSharedPreferences("purchased", 0).getBoolean("purchased",false) == false) {
   admob();        
   Log.d("NOT UPGRADED", "SHOW AD AS UPGRADED ALREADY");
} else if (getSharedPreferences("purchased", 0).getBoolean("purchased",true) == true) {
   admobskip();    
   Log.d("UPGRADED", "DO NOT SHOW AD AS UPGRADED ALREADY");
}

10 Comments

== true is a tautology.
@Bathsheba just to make it clear to him as he's new to Java ;)
Thank you for this, you answered without prejudice, unfortunately some of the other guys have been a bit harsh considering I am new to this, thank you once more! :)
Come on, we're a nice bunch of folk ;-)
@Alamri: Absolutely I agree. Too many folk are downvoting on the grounds of "obviousnesss". That is never a reason to downvote. And it's getting steadily worse.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.