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 trialMichelle Mangwiro
Courses Plus Student 467 PointsReceiving input- Java Basics
Where am I going wrong on the third task?
// I have imported java.io.Console for you. It is a variable called console.
String firstName = console.readLine("michelleMangwiro");
String lastName = console.readLine("%s");
console.printf = ("First name:"%s);
4 Answers
Steve Hunter
57,712 PointsHi Michelle,
The third task wants you to place a string inside an output string. You have correctly isolated the %s
as being the way to do this. What you need to do is put that %s
inside the string, where you want the contents to be inserted. Then, outside of the string, you add a comma, followed by the variable name you want inserting there.
String firstName = console.readLine("What is your first name?: ");
String lastName = console.readLine("What is your last name?: ");
console.printf("First name: %s", firstName);
console.printf("Last name: %s", lastName);
The challenge looks like the above, once complete.
I hope that helps.
Steve.
Justin Kraft
26,327 PointsRemove the string parameter within console.readLine() and use the variables as before when using printf
String firstName = console.readline(); String lastName = console.readLine() console.printf("First name: %s, Last name: %s", firstName, lastName);
The console should wait for two user inputs in this case.
Justin Kraft
26,327 PointsAs Steve mentioned inserting a string within console.readLine() prints a prompt to the console.
Madison Finck
4,645 PointsHope this helps:
Your code : console.printf = ("First name:"%s); You need a space between the colon and percent sign, and after the %s you need a \n . Lastly after that you need firstName . My code: console.printf("first name: %s\n", firstName);
Michelle Mangwiro
Courses Plus Student 467 PointsThanks
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsSo the line:
console.printf("First name: %s", firstName);
inserts the value held inside the variable
firstName
at the position marked with%s
.