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 with Java Basic Android Programming Choosing a Random Fact

Maan Azzam
Maan Azzam
2,239 Points

I used a txt file in ASSETS to take facts from. But it only gets the first line! HELP

So i've created a txt file with facts inside, and i wanted to take each LINE and store it in an array to display it on the screen, but my code just shows the FIRST line of my txt folder and stops. does anyone know how to make it continue untill the file ends.

*NVM FIXED IT SOLUTION : i changed factTextView.setText(factsforapp[0]) to the random number generator so i added *

    public String getFact() {
        // Randomly select a fact
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(factsforapp.length);
        return factsforapp[randomNumber];
    }

boldhere is my code*bold*:

package funFactsApplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import funFactss.funFactsApplication.R;

public class FunFactsActivity extends AppCompatActivity {
    public static final String TAG = FunFactsActivity.class.getSimpleName();
    private FactBook factBook = new FactBook();
    private ColorWheel colorWheel = new ColorWheel();
    private TextView factTextView;
    private Button showFactButton;
    private RelativeLayout relativeLayout;
    String[] factsforapp = new String[1000];

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_facts);

        //Assign the Views from the layout file to the corresponding variables
        factTextView = findViewById(R.id.factTextView);
        showFactButton = findViewById(R.id.funFactButton);
        relativeLayout = findViewById(R.id.relativeLayout);
        loadWords();

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Update the screen with our new fact
                factTextView.setText(factsforapp[0]);
                // Update the color of the background
                int color = colorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                showFactButton.setTextColor(color);
            }
        };
        showFactButton.setOnClickListener(listener);

        //Toast.makeText(this, "Yay! Our Activity was created!", Toast.LENGTH_LONG).show();
        Log.d(TAG, "We're logging from onCreate() method!");
    }
    public void loadWords()
    {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("factsforapp.txt")));
            for(int i = 0;i<factsforapp.length;i++)
            {
                factsforapp[i] = br.readLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 Answer

Hi, Your program looks fine. But I think problem is in this line "factTextView.setText(factsforapp[0]);" Here you are asking the program to display first fact only. I think it should like that: String fact = factTextView.getFact(); factTextView.setText(fact);