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

String variable

Create a String variable named intAsString. Convert the randomNumber integer to a String value and store it in the intAsString variable.

RandomTest.java
Random randomGenerator = new Random();
Random randomNumber = new Random();
int RandomNumber = randomGenerator.nextInt(10);

String variable = "inAsString";

help me please, i am stuck here. i am new to this coding and this is a task to complete before i can proceed

4 Answers

Anson Tio
Anson Tio
5,448 Points

Let's solve it step by step.

  1. "Create a String variable named intAsString." In Java, to create a String type variable.

    // Example
    // String <variableName> = "<inputString>"
    // remember double quotes for a string value
    String aString = "Hello";
    // or 
    String anotherString = "";
    // you can declare a variable without assigning any value
    String yetAnotherString;
    
  2. "Convert the randomNumber integer to a String value" Please have a look on java documentation link I provided https://docs.oracle.com/javase/tutorial/java/data/converting.html .Since randomNumber is an integer, We can use toString() function from Integer class.

    String randomNumberInString = Integer.toString(randomNumber);
    
  3. "store it in the intAsString variable." Since the question asked you to store it in "intAsString" variable, we can do this

    String intAsString = Integer.toString(randomNumber);
    

Hope this helps :D

QUESTION:-

Create a String variable named intAsString. Convert the randomNumber integer to a String value and store it in the intAsString variable. Hint: Add them together like in the video!

Thank you for your response!! but unfortunately it didnt work. :( I am having a really hard time understanding this part. the video had some use of a plus sign. i dont quiet get it.

LINE OF CODE FOR PREVIOUS QUESIONS:-

Random randomGenerator = new Random (); Random randomNUmber = new Random (); int RandomNumber = randomGenerator.nextInt(10);

(THE STRING CODE GOES HERE)

can you use the ACTUAL name of the string i have to use for the example please in regards to the question?. for example if it was a question posted to you how would you answer?.

thanks much

Anson Tio
Anson Tio
5,448 Points

Maybe you can try this one

// base on your code above
Random randomGenerator = new Random();
Random randomNumber = new Random();
int RandomNumber = randomGenerator.nextInt(10);

// I just noticed that you mentioned about plus sign, so maybe try this
String intAsString = randomNumber + "";

Haha thanks mate