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 (2014) Basic Android Programming Using the Random Class

Robert Harrowfield
Robert Harrowfield
625 Points

Challenge Task 4 of 4

Task is: Initialize a new Random variable called randomGenerator using the Random() constructor. Hint: Don't forget to use the new keyword. But for some reason this code is not working on the website but works in Android Studio. There is also no skip button to get past this Task so i have been stuck on this question for 3 weeks without any luck.

RandomTest.java
String intAsString = "";
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
intAsString = randomGenerator + "";

7 Answers

Jordan Ernst
Jordan Ernst
5,121 Points

simple syntax error, you have random\Generator as the variable where it should be randomNumber. quick fix and the code is good!

Ian Burres
Ian Burres
250 Points

So you want to create a random number from 0-9, right? If you have imported import java.util.Random; then it should be fine. However, if you are trying to randomly generate a string you will run into a typecast issue. Let me which one you are trying to randomize, and I will walk you though the problem (without giving you the answer).

naiara gonzalez
naiara gonzalez
16,778 Points

String intAsString = ""; Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(3); intAsString = randomGenerator + ""; THIS works for me!!

Ian Burres
Ian Burres
250 Points

Ok, I did the challenge and got it to work. I'll be on for a little while longer, so if you'd like some help let me know.

Ian Burres
Ian Burres
250 Points

Try not to "give" the answer away Jordan....:)

What he means is this...

Random randomGenerator; Random rand = new Random(); rand.nextInt(10);

Robert Harrowfield
Robert Harrowfield
625 Points

https://db.tt/pjgF9LdH

So this is code from my Android Studio which works.

As you can see the only thing different is called "fact" not "intAsString" and random is (3) not (10)

This WORKS in android studio, its the STEP by STEP from the video before this question, i put this code into the "challenge" on the webpage and it doesn't work.

Thank you Jordan for explaining the error, as im not really trying to "learn" the way it needs to be for myself as it already WORKS on android studio and "if it aint broken dont fix it" seems to work well for me in the past so i was mainly looking for a way to get the website to accept this, which is weird as i get there are more then one way to do things with coding but the video teachs one way and the website seems to need another way to make it work.

that was the problem i was having but thanks to everyone for responding to my question so quickly, its taken me a few days to get back on here.

Jordan Ernst
Jordan Ernst
5,121 Points

wait until you progress my friend the code challenges get progressively more challenging. the way it works is the video's develop the idea and the challenge forces you to use the concept with new variables and methods. it's actually super helpful. so yeah you will need to pay close attention that you understand what is happening.

Ian Burres
Ian Burres
250 Points

Robert, I took time out of my busy schedule to help you solve this problem, and the answer I provided was correct given the challenge you provided. Unfortunately, I do not believe you understand the core concepts involved here. Programming is not about learning little "tricks". If you are serious about programming as an occupation, understand that you will be expected to take programming tests and explain fundamental concepts during interviews. Please do not take offense to this, as I am only trying to give you good advice.