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 trialBJORN HERMANSEN
Courses Plus Student 171 PointsAndroid Dev
I'm new to Android Dev so I'm not familiar with any of the information. Here's what I'm being asked to do
"Declare an int variable named 'randomNumber' and, without setting a range, use the randomGenerator variable and its nextInt() method to get a random number."
Below is what I've written per what I've already written in Eclipse with Ben...
Random randomGenerator = new Random(); //Construct a new Random generator int randomNumber = randomGenerator.nextInt(3); answer = Integer.toString(randomNumber);
//Update the label with our dynamic answer
answerLabel.setText(answer);
I have to be honest, I don't follow at all... I've been referencing the code that Ben has instructed to me write in Eclipse but it's not helping that I'm not understanding what I'm writing. I get that Java is Object-Oriented, that makes sense, but attempting to complete the task given above has me stuck in the mud.
I've watched Ben's video 3 times now and I'm still here. Any help would be greatly appreciated.
Thanks
3 Answers
Gunjeet Hattar
14,483 PointsHi BJORN,
"Declare an int variable named 'randomNumber' and, without setting a range, use the randomGenerator variable and its nextInt() method to get a random number"
In the first part of the challenge you must have written the line
Random randomGenerator = new Random();
Random is a special feature built inside Java that helps you get random numbers up until a specified number.
Now the second part
Random randomGenerator = new Random();
//declare a variable called randomNumber
//I am breaking this to help you explain but as given by Jose above you can write it in one line too
int randomNumber;
//without setting a range, use the randomGenerator variable and its nextInt()
randomNumber = randomGenerator.nextInt();
You first declare an integer variable called randomNumber. Pretty straightforward.
Next, as mentioned above Random is an inbuilt java feature and it has a special method called nextInt that fetches a new random number each time it is called. This method has a basic syntax
nextInt(range); //where range is the number up until where you wish to get the random number
So using the Random variable called randomGenerator we call the nextInt method, but as specified in the instruction we do not set the range and thus it is blank
A lot of the basics reflect back to core java. Though I am not sure if you are well versed with it but just a quick read will suffice.
Hope that helps. Ask again if you need help
Jose Beltran
1,195 Pointsint randomNumber = randomGenerator.nextInt();
BJORN HERMANSEN
Courses Plus Student 171 PointsThank You very much! I feel better moving forward now.