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


Eleni Minadaki
Eleni Minadaki
3,687 Points

something goes wrong with the loop...

Hi treehousers! i am creating an app based in Build a simple android app,"fun facts." The problem i have is that is my FactBook.java class is not change insequence (from 1 to 2 to 3 etc...).The only different thing i have make from the lessons is that i have add 31 text, because i try to make something like diary of the month. i put my code below so you can understand better,just instead of my 31 texts i put 5 ".....". i hope someone to know about this! Thanks!

public class FactBook {

    public String [] mFacts = {

    public String getFact(){
        String fact = "";
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(mFacts.length);

        fact = mFacts[randomNumber];

        return fact;



2 Answers

The reason your code isn't running in sequence is because you are still calling a random number to be generated and used to pull a fact. If you want the facts to go in order from 1 - 31 or whatever, you would need to use a for loop and store the number of the fact somewhere in your program in another variable.

Eleni Minadaki
Eleni Minadaki
3,687 Points

Hello Jeremiah, thanks a lot for your reply. This you tell me seems the solution, just because i am new in android and in java,i follow step by step the lessons and its little confused for me what should i delete and what should i write and where. But i know to make the for,so i 'll try and could i make an other question to you if i need later?

Eleni Minadaki
Eleni Minadaki
3,687 Points

Hello again Jeremiah, your suggestion to make a for loop help me a lot But as i expect i stuck because something i have to delete or to add in my main or in another class and i don't know what. If you can again to help me i appreciate! 1) The problem i have know is that when the app runs the colors change with the random But the text didn't, 2) i delete the random from the factBookClass and create a for. 3)Below i will write my code for i) my main with the name ThoughtOfTheDayActivity.java ii)the FactBook.java and iii)ColorWheel.java (the only problem in colorwheel is that the color in the end " int colorAsInt = Color.parseColor(color);" is red but i don't have a warning why) as i told you the previous time i have write 31 articles for each day,something like a diary But instead of 31 i will write 5)......; for don't be more tedious. Thanks a lot for any help you could give me.

FACTBOOK.JAVA public class FactBook { public static void main(String [] args){ String[] mFacts; mFacts = new String[5]; mFacts[0]...; mFacts[1]...; mFacts[2]...; mFacts[3]...; mFacts[4]...;

int i; for(i=0; i<mFacts.length; i++) { System.out.println(mFacts[i]);


public String getFact(){

    String fact = "";
    //Random randomGenerator = new Random();
   // int randomNumber = randomGenerator.nextInt(mFacts.length);

   // fact = mFacts[randomNumber];

   return fact;


import android.app.Activity; import android.graphics.Color; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast;

import java.util.Random;

public class ThoughtOfTheDayActivity extends Activity {

public static final String TAG = ThoughtOfTheDayActivity.class.getSimpleName();

private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();

protected void onCreate(Bundle savedInstanceState) {
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    final Button showFactButton = (Button) findViewById(R.id.showFactButton);
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View view) {
        String fact = mFactBook.getFact();


            int color = mColorWheel.getColor();

 //Toast.makeText(this, "Yay!our activity was created!", Toast.LENGTH_LONG).show();
    Log.d(TAG, "We are login from the onCreate()method!");



import android.graphics.Color;

import java.util.Random;


  • Created by Eleni on 30/4/2015. */ public class ColorWheel {

    public String [] mColors = {

        "#13A1CB", //20


    public int getColor(){ String color = ""; Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(mColors.length);

    color = mColors[randomNumber];
    int colorAsInt = Color.parseColor(color);
    return colorAsInt;



So you want to get your facts in ascending order? E.g.

  • first call of getFact should return "1)........"
  • 2nd call => "2)........" -3rd call => "3)........" -... and the 6th call should start from the beginning?

If so, there is no need for random numbers and you should implement your specific logic probably this way:

  • decide where the first call should start
  • create a holder var for the next getFact call, so it will know where to continue
  • update this var in the getFact
Eleni Minadaki
Eleni Minadaki
3,687 Points

Hello janbaran, thanks a lot for your anwer!