Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Coding the Fun Facts Using Conditionals (else Statements)

mahesh gurbaxani
mahesh gurbaxani
1,420 Points

Please help

public void onClick(View view) { String fact = ""; int radInt = 0;

                // Random randomNumber = new Random();
                //  radInt = randomNumber.nextInt(2);
                if (radInt == 0) {
                    fact = "we are all happy";
                    Label2.setText(fact);
                } else if (radInt == 1) {
                    fact = "what a lovely day";
                    Label.setText(fact);
                    relativeLayout.setBackgroundColor(Color.WHITE);
                    myButton.setTextColor(Color.RED);
                }
            radInt++;



            }

Instead of using the Random object, I have recoded the code as shown above. But id does not work. After the first 'if' statement is activated, the next 'if' does not come into play. Can you please help ?

6 Answers

mahesh gurbaxani
mahesh gurbaxani
1,420 Points

In the funfacts activity, I would like the fun facts to come in a sequence , rather than in a random order. How do I do that. Would be grateful for any tips. Thanks

Sai Sushanth Durvasula
Sai Sushanth Durvasula
2,759 Points

See the if-else-if functinality once. radint can be either 0 or 1 not both at atime so if radint==0 it enters frits if and if radint==1 it enters second if .Only one of the ifs execute at a time.

mahesh gurbaxani
mahesh gurbaxani
1,420 Points

Thanks. I guess the do..while loop should work here ?

Sai Sushanth Durvasula
Sai Sushanth Durvasula
2,759 Points

Can you explain what is that you want to achieve??

Sai Sushanth Durvasula
Sai Sushanth Durvasula
2,759 Points

First you can create a fact array as follows:

String fact[] = {"hii","hello","how are you?"};

Then to display in a sequence you can use a for loop as follows:

for(int i=0;i<fact.length();i++)

{

Display.setText(fact[i]);

//waiting time

int j=10; // can be changed to required time

while(j>0)

j--;

}

Hope this helps!