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

Java Java Basics Using your New Tools Multiple Format Strings

joseph bonsignore
joseph bonsignore
779 Points

madlibs output

I'm doing the final task for the madlibs challenge in java basics. I don't know what to do. Please help.

Multiple.java
// I've imported java.io.Console for you.  It is stored in a variable called console for you.
String name = console.readLine("Enter your name: ");
String pastTenseVerb = console.readLine("Enter a past tense verb: ");
%s; really %s; this coding exercise;

1 Answer

Cameron Hastings
Cameron Hastings
3,439 Points

Hey, looks like you got the first and second task completed. You were close in the third one. I'll go through all the tasks in case others are stuck too.

So, here is task one:

"Create a new variable called name and accept input from the console using the readLine method."

So in the video, you would declare a variable String name = ...... and then set that String variable (a bunch of text) equal to something. In our case, we want to use the console.readLine("What you enter here will be the value of name: "); So here is what we enter for task 1:

Solution to task 1:

Multiple.java
String name = console.readLine("Enter your name: ");

For task two:

"Create a new variable to store a past tense verb, so in camel case that is pastTenseVerb. Accept input from the console using the readLine method."

We are basically doing the same thing as task one. All we need to do is create a new String variable that allows the user to enter in a past tense verb. Your code should now look like this:

Solution to task 2:

Multiple.java
String name = console.readLine("Enter your name: ");
String pastTenseVerb = console.readLine("Enter a past tense verb: ");

Task three

"Output a sentence that takes both the name and past tense verb using a single statement. It should look like this: name really past tense verb this coding exercise."

Now we need to output a message though the console. Like in the video, we start with console.printf(" ");. We then have to add our message and variables. To add our variables within the text, we use a %s. After you put in the message, you have to say what variables you want to replace the %s's. Do this by separating them with commas. Your code should look like this in the end:

Solution to task 3:

Multiple.java
// I've imported java.io.Console for you.  It is stored in a variable called console for you.
String name = console.readLine("Enter your name: ");
String pastTenseVerb = console.readLine("Enter a past tense verb: ");
console.printf("%s really %s this coding exercise.", name, pastTenseVerb);

That's it. I hope this helped.