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
Jeff Hyatt
10,764 PointsFun Facts Activity. Can I remove the random number generator and instead increase the integer by 1 every click?
Hi, I am new to Android app development but have just completed the first course in the Android Development Track. I would like to change the way my application works so rather than generating a random number, the button click will increase the current int value [0] by 1 and then increases it again with every click.
I currently had the random number generator commented out so I can try to figure out how to increase the value of mFacts[0] by 1 every click however I am having a hard time finding out how to do this. Here is my current code. Thank you for any input.
public class FactBook {
//Member variable (properties about the object)
public String[] mFacts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built.",
"Treehouse is not actually in a tree." };
//Method (abilities: things the object can do)
public String getFact(){
String fact = "";
// Randomly select a fact
//Random randomGenerator = new Random(); //Construct a new random number generator
//int randomNumber = randomGenerator.nextInt(mFacts.length);
fact = mFacts[0];
return fact;
Within my FunFactsActivity.java my onClick code is as follows.
public void onClick(View view) {
String fact = mFactBook.getFact();
//Update the label with our dynamic fact
factLabel.setText(fact);
1 Answer
Henry Goh
20,201 PointsHi Jeff,
You could create a counter private int currentFact = 0; at the same place where you placed your mFacts variable.
In your getFact() method, you can do fact = mFacts[currentFact];.
At the end of the getFact() method, before the return statement, you will need to do a check if the counter is smaller than the size of your mFacts array by adding an if statement at the end.
```if(currentFact < mFacts.length)
{
//Increase counter by 1
currentFact++;
}
else
{
//Reset counter to 0
currentFact = 0;
}
This will avoid your program from getting an ```ArrayIndexOutOfBoundsException```.
I hope that works for you, and if you do not understand what is going on, I'll try my best to explain in more detail.
Best regards,
Henry
Jeff Hyatt
10,764 PointsThank you so much Henry! Your help is appreciated greatly!
Jeff Hyatt
10,764 PointsI've got an additional question, I hope you don't mind. I successfully declared private int currentFact = 0; in my FactBook.java along with my mFacts variable. I am having issues in my onClick() method however. I am slightly unsure whether or not to use this.currentFact++; or just currentFact++; but anyways here is my current onClick code.
public void onClick(View view) {
String fact = mFactBook.getFact();
//Update the label with our dynamic fact
factLabel.setText(fact);
currentFact++;
if(currentFact < mFacts.length) { currentFact++; } else { currentFact = 0; }
int color = mColorWheel.getColor();
relativeLayout.setBackgroundColor(color);
showFactButton.setTextColor(color);
The errors I am getting are cannot resolve symbol 'currentFact' and cannot resolve symbol 'mFacts'. I have been experimenting with ways to get this problem solved but as of now I haven't found the solution. Thank you greatly for any input you can provide, Henry. And thank you for what help you've already given me.
Henry Goh
20,201 PointsHi Jeff, Sorry I did not read your code carefully and did not notice you actually have two clases. I edited my answer above so you can reference it again.
As for your second question, it is not necessary to put the this keyword unless, for example, you have the same name for your instance variable and local variable.
The this keyword references 'this' object.
Some programmers like to use this as it is easier for them to know which variable they are referencing. As for me, I think it will affect the program's performance if used too many times.
If I am not wrong, I believe the this keyword actually will reference the current object first, then go in deeper and retrieve the value of the current object's variable.
I hope that helps, best regards,
Henry
toddschneider
4,795 Pointstoddschneider
4,795 PointsGood Question Jeff I am trying to do this my self!