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 (retired 2014) Learning the Language Objects and Random Numbers

word by word explanation

The video covered most of this, but some parts were quite quick. It would be great to have a point by point breakdown for each element of this code as a reference. 1: String means... 2: Answer means... 3: =""'; means... 4: Random means... 6: RandomGenerator means... 7: etc, etc, etc.

String answer = ""; Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(3); answer = Integer.toString(randomNumber);

Something like this http://www.tutorialspoint.com/java/java_methods.htm

1 Answer

Theodore Kruczek
Theodore Kruczek
8,800 Points

Let me take a stab at this for you. Just woke up so you may have questions lol.

  1. String - Establishes that we want to create a new variable and that its type will be a string. Think of this as a string of characters, or text).

  2. answer - This is the name of our variable. You can name it anything as long as the first character is a lowercase letter.

  3. = - Set this new variable named answer to equal whatever comes next.

  4. "" - This sets the value of answer to a non-null value of nothing. Think of null as outer-space, a vacuum of nothing. Where as "" is more like an empty glass on earth. We call both nothing, but when a variable equals "" it is officially there, just empty.

  5. ; - End line.

  6. Random - This is like string, but in this case the new object we are creating is a Random object.

  7. randomGenerator - We are naming our Random new object, again you can name this anything.

  8. = - Set the value of randomGenerator to whatever comes next.

  9. new Random() - Here we are telling Android to create a new Random object and randomGenerator will be how we interact with this new object.

  10. ; - End line.

  11. int - Creating a new integer variable, same as String or Random.

  12. randomNumber - Name of our integer, you can make it anything.

  13. = - set the value of randomNumber to what comes next.

  14. randomGenerator.nextInt(3) - All Random objects have methods available to them. We want to access the "nextInt" method of our Random object. This will return a random integer value (whole number) between 0 and whatever parameter is passed to it minus 1. So by passing a value of 3 to our randomGenerator's nextInt method, we return a random value between 0 and 2.

  15. ; - End line.

  16. answer = - Set the value of our answer string to whatever comes next.

  17. Integer.toString(randomNumber) - We are accessing the Integer class' method "toString" which allows us to convert an integer (number) in to a String (text). When talking to a computer, who is overly literal about everything, if you tell it that the variable it is using is a String (text) it cannot use it for math and visa versa. "I can't add that, you said it was a word not a number?" So while we know "2" can be both a number and used for writing in a sentence, the computer has to be told explicitly how to use "2". In this case we have a number, but we want to use it for a label which requires a word. This method will quickly convert whatever randomNumber's Integer value is into a String of the exact same value.

  18. ; - End line.