## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today. # facts.length()

Does the method length() for object facts starts from 1 to "length of array" or 0 to 'length of array - 1"?

I am confused about the facts[randomNumber] array. Any help will be much appreciated. You are welcome !!!!

See you in the forum ...

Take a look at the java course track here at treehouse ... I am sure you will find a lot cool stuff there ... and Craig is awesome :) ```String[] facts = {"one", "two", "three", "four"};
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt((facts.length));
// the number generator will give you an int between 0 and 4
// thats why you need to tell the generator how many items are inside of the array
```

But what if the generator gives you number 4 ... this will give you an error. There is no item on index 4 inside of the array. On index 3 is the String "four". So you can modify your code like this:

```int randomNumber = randomGenerator.nextInt((facts.length) - 1);
// this will give you a number between 0 - 3
```

So you can extract a random String from your array using this line:

```System.out.println(facts[randomNumber]);
```

Make sense? Hope this helps

```String[] facts = {"one", "two", "three", "four"};
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt((facts.length));

//facts.length returns 4 because there are 4 elements in the array.
//the randomGenerator will give you an int between 0 and 3 (4 possible values)
//These numbers correspond to each element in the facts array
//"one" = 0
//"two" = 1
//""three"" = 2 etc.
```
```// Displays the fact on the screen.
factTextView.setText(facts[randomNumber]);
// The array index values match exactly the random number values
// For example, if randomGenerator returns 3 (the maximum possible value),
// then facts is selected, which corresponds to "four"
``` Hi Trainer,

thank you for the clarification :) You are totaly right !

this line:

```int randomNumber = randomGenerator.nextInt(facts.length);
```

or this line:

```int randomNumber = randomGenerator.nextInt(4);
```

They both return a number between 0 and 3 !!! I said that they return a number between 0 and 4, so 4 would be also possible and this is not correct. I hope that my explanation, despite of my error, could help understand the logic, Premkumar !

Sorry for the confusion and thanks again to Trainer !

Thumbs up Hi Trainer,

Thank you, now I get it, even though the numbers generated by the random code is from 0-3 and facts.length is 4, but they both represent the same amount of number. So that explains a lot!

Thank you again. Appreciate that. Hi Premkumar,

the length method gives you the number of items of an array.

If you have an array like this:

```String[] stringArray = {"itemOne", "itenTwo", "itemThree"};
```

stringArray.length would give you 3. Because there are three elements in the array.

Does it make sense? Hi Grigorij,

Thanks. Now that makes sense. But now if we look at my code:

String[] facts = {"one", "two", "three", "four"}; Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(facts.length); facts[randomNumber];

This facts.length should give me values of 1,2,3,4 where as the array index points to 0,1,2,3. Will that give a bug?

Am I correct?