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 trialMark Fremer
Courses Plus Student 171 PointsIs this is a glitch? unable to complete task/quiz question help?
I go threw the instructional video for "Objects and Random Numbers" in: ANDROID DEVELOPMENT > BUILD A SIMPLE ANDROID APP > LEARNING THE LANGUAGE
Which does not explain how to limit a range nor explain what an "exclusive upper bound" is.
I am on task 3/4 of the quiz and am stumped on this question:
"Now limit the range of the randomNumber variable from 0 to 9. Remember that the parameter for the nextInt() method is the exclusive upper bound."
The correct answer to my previous question "Declare an int variable named 'randomNumber' and, without setting a range, use the randomGenerator variable and its nextInt() method to get a random number."
was:
Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt();
Is this a glitch? What would the answer be it was unexplained in the video... ??
20 Answers
Ben Jakuben
Treehouse TeacherOkay, so let's break down the instructions piece by piece:
"Declare a String variable named intAsString."
So we need a variable named intAsString
that has the data type of String
, i.e.:
String intAsString;
"Convert the randomNumber integer to a String value and store it in the intAsString variable."
So check out that line you pasted from the video:
answer = Integer.toString(randomNumber);
This calls the toString
method of the Integer
class to change randomNumber
into a String and store it in the answer
variable.
We're doing the exact same thing in this exercise, but we are replacing answer
with intAsString
. We are now saying this:
"This calls the toString
method of the Integer
class to change randomNumber
into a String and store it in the intAsString
variable."
With that hint, what needs to be replaced in that line of code?
Shawn Bild
1,358 PointsI'm 100% green to Java. So when you introduced a new variable and ask me to do something with it, I was really thrown for a loop. I have been working like 15 minutes on this one question.
Ben Jakuben
Treehouse TeacherJordan Finnigan, glad you found help here! Remember to check the forum as you work through the projects, and you can also email us at help@teamtreehouse.com if you get stuck. Hope you get a lot out of the Android projects!
Ben Jakuben
Treehouse TeacherHi @Mark,
Thanks for your feedback about this question! I went and reviewed the transcripts for my videos because I do talk about the "exclusive upper bound"......but not until the beginning of the next video. I must not have realized the timing when I wrote this code challenge.
I'll take a look at how best to fix this later this week, but for now, for you, you just need to plug in the correct parameter for the nextInt()
method to get 10 numbers (0-9). "Exclusive upper bound" means that it's the upper bound of the range of numbers, but it's excluded from the actual range. So what would that be for the range 0 to 9?
Ben Jakuben
Treehouse TeacherWhat is the data type for intAsString
? Remember that in Java, every variable needs to be declared as some sort of data type (String
in this case). Check out my post two spots above yours in this thread for more help.
Also note that the toString
method requires an int
as a parameter. What int
variable is already there in the Code Challenge?
Edward Fatula
3,666 PointsAs you point out, the problem stemmed from not having the data type on the line of code. In our Eclipse example, we had previously declared "answer" as a String variable, and so it wasn't necessary to have the declaration on the line "answer = Integer.toString(randomNumber);" Since in the Code Challenge we had not previously declared the variable "intAsString", the syntax error persisted. That solved the issue!! Onward!
Mark Fremer
Courses Plus Student 171 PointsWell counting the 0 all the way to 9 you would get 10 numbers correct or would that be 0 minus 9 getting -9?. Not exactly sure what you mean by range.
Mark Fremer
Courses Plus Student 171 PointsOk i figured it out the answer was 10 i should have thought about how many numbers there where but I am stuck trying to understand the last question before the next video "Declare a String variable named intAsString. Convert the randomNumber integer to a String value and store it in the intAsString variable. Hint: Use the toString() method of the Integer class." I am unsure what the correct syntax would be as this was not in the previous video either.
Ben Jakuben
Treehouse TeacherGlad you figured it out! This next one actually was covered in the video. Check it out starting at the 4:37 mark.
Mark Fremer
Courses Plus Student 171 PointsAll i know to do is write it out this way answer = Integer.toString(randomNumber);
and i have no clue where intAsString goes i have tried different things answer = intAsString(randomNumber); answer = Integer.toString(intAsString); answer = intAsString.toString(randomNumber); answer = Integer.intAsString(randomNumber); intAsString = Integer.toString(randomNumber); int = Integer.toString(randomNumber);
Mark Fremer
Courses Plus Student 171 PointsI wasn't sure if the question was asking for string answer; also but it appears it does, that was my problem. Thank you for explaining. :)
Juan Guerrero
3,994 PointsI'm a bit lost myself
intAsString = Integer.toString(intAsString);
Juan Guerrero
3,994 PointsAwesome that helped jog my memory. Thanks
Shawn Bild
1,358 PointsNever mind, that post did help me when you broke down the structure..
Marcos Melone
5,220 PointsI'm caught in the challenge of Java have seen the answer to Mark, but still going wrong I must have misunderstood the question or whatever, I understand "intAsString" goes in place of "answer" but soon realized it was not so accurate help me pass this stage, I'm from Brazil and language also impedes a bit for me because I'm not fluent in English and can be confusing something, help me please ...
Ben Jakuben
Treehouse TeacherHi @Marcos,
Sorry you're having trouble! Can you post your code in here for us to take a look at? You can either add a comment in here or make a completely new post.
Jordan Finnigan
1,637 PointsJust as a heads up, I also had a lot of trouble with the 4th task in this quiz. Had I not found this specific help article, I don't know if I would have been able to complete it.
Milo Joseph
3,121 PointsIm still lost this is confusing.
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
intAsString = Integer.toString();
Ben Jakuben
Treehouse TeacherHi Milo,
The Integer.toString()
method requires a parameter. Right now without one, it doesn't know what integer you are trying to convert to a String. You need to pass in the randomNumber
int that you just generated to that method.
Also, did you "Declare a String variable named intAsString"? You are using it there, but you need to have a line that defines it somewhere before you use it, i.e.:
String intAsString;
Carl Taggett
10,319 PointsThis help me a ton thanks
Ricky Sparks
22,249 PointsThat makes sense. Good thing I Googled this question today