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 trialNicholas Demetriou
639 PointsChallenge Task 2 of 3 on Arrays
Having difficulty with this task
Declare an int variable named numberOfSports and set it to the number of elements in the array using the array's length property.
String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = "Basketball";
9 Answers
peter backlund
6,910 PointsYou were close before, I think this is what you're looking for:
int numberOfSports = sports.length;
Mikael Enarsson
7,056 Pointslength
is a property of an array, and thus is accessed using arrayName.length
. There are also a couple of other problems, however that's the main one. But, if you have further problems after this, don't hesitate to ask ^^
Nicholas Demetriou
639 Pointsi don't get it why your initial answer didn't work...
peter backlund
6,910 PointsSo I confused Python and Java syntax, you can't access the length property of an array in Java using the length()
method like you can in Python. I really do apologize for making that whole process way longer than it needed to be. Good luck moving forward!
Nicholas Demetriou
639 PointsI corrected the task instruction
Nicholas Demetriou
639 PointsIt doesn't run
It the emulator i have this: int randomNumber = randomGenerator.nextInt(facts.length);
peter backlund
6,910 PointsHave you imported java.util.Random
? Can you post the whole file please?
Nicholas Demetriou
639 Pointsimport android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class FunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Declare our View variables and assign the views from layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] facts = {
"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 a tree" };
// The button was clicked, so update the fact label with a new fact
String fact = "";
// Randomly select fact
Random randomGenerator = new Random(); // Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(facts.length);
fact = facts[randomNumber];
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_fun_facts, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
peter backlund
6,910 PointsHmm, I didn't have any problem with the code you gave me. Try Rebuilding
it under the Build
menu. If that doesn't work, post the output from the logcat. We'll get it figured out.
Nicholas Demetriou
639 Pointsi don't have a problem with the code, i can't figure out the second Array task
Do i need to put
Random before the int variable
https://teamtreehouse.com/library/build-an-android-app/coding-the-fun-facts/using-an-array
peter backlund
6,910 PointsOk, that was my fault for not looking at where you were at. No you don't need the random. You're just looking for the length, or number of elements in the array. So you want to use int numberOfSports = sports.length;
Nicholas Demetriou
639 Pointsi had length.sports all this time...
great thanks
Nicholas Demetriou
639 PointsOk
yesid ramirez
533 Pointsyesid ramirez
533 PointsThank you very much. God bless you Muchas Gracias. Dios lo bendiga