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 an Interactive Story App (Retired) Finishing the User Interface Review: Adding Story Pages

Kevin Gresmer
Kevin Gresmer
3,020 Points

Complete this code. We want to format the 'jokeText' String to include the value of the 'animal' variable.

Complete this code. We want to format the 'jokeText' String to include the value of the 'animal' variable. 'jokeText' already has the appropriate placeholder.

String animal = "monkey"; jokeText = String.format( ???, ??? ); mTextView.setText(jokeText);

This seems like a simple question. Maybe I am missing something with this concept.

I think the answer should be %1$s, animal, but this is incorrect. Can someone explain this to me?

Thanks,

1 Answer

Dan Johnson
Dan Johnson
40,533 Points

It mentions that 'jokeText' already contains the placeholder, which means it's setup as the format string. If we pretend it looks like this:

String jokeText = "Animal: %s";

Then we could insert the String 'animal' into it like this:

jokeText = String.format(jokeText, animal); //This would produce the String, "Animal: monkey"