Java: How to Use an If/Else Statement

Header

Last week we covered how toast happens in Android Studio. Today, we’re going to make it a tad more dynamic with an if/else statement. Going back to this Riddler App, I want to add hints for the user if they can’t guess the answer outright. Quick reminder, our test riddle was “What has a neck but no head?” Most people start guessing insects for that one (isn’t an earthworm basically all neck? you might argue), but the answer we’re looking for is “A Bottle.”

To keep the mystery alive, we’re going to keep hints semi-living creatures related. If a user hits the Hint button once, a toast message will pop out reading, “It has no fingers or toes either.” Otherwise, the toast will say, “And lives all over the world.”


What is an If/Else Statement?

Those are formatted like so in java:

if (condition) {
do this piece of code;
} else {
do this piece of code;
}

Conditions can be formatted in a lot of different ways. Sadly, these are rooted in math. Math is evil, but luckily, this is only going to require a really simple equation. Some of the simplest you can use are these:

>       Greater Than
<       Less Than
==     Equal to

We’re going to grab the == for ” is equal to” and use a hint_counter variable to tell Android if the button has already been hit.

Let’s start with the hint_counter.

Declaring a variable in java is pretty straightforward. Like a method, you’ll start off with visibility. We’re going to keep ours public. The next part is data type. Since we’ll be using it in our conditional statements, it’ll need to be an integer (or int).

public int hint_counter = 0;

And we’ll set it to 0 to start. That way, if the variable is 0, the app will know use the code within the first condition, and once it gets higher, it’ll go to the second.

Hint_Step_1

Next, we’ll set up the first condition. This will go inside the showToast method that we created last week. To make things simple, I’m hardcoding the text here in the java file, but you could also stash the hint inside the strings.xml file and refer to it as R.strings.whatever_you_called_the_hint.

Hint_Step_2

Once that first condition has run, we’ll increment hint_counter up by one. In java, that looks like this:

variableName++;

That semicolon is important, so don’t forget it.

Hint_Step_25

After that, we’ll set up the second condition, showing the other toast message. By putting this inside an else {} clause, we’re basically letting the program know that if hint_counter is anything other than 0, it should run that block of code.

Hint_Step_3

And that’s it. Here’s what that looks like when it’s loaded.

Click_1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s