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 trialSiddhant Dahiya
156 PointsGetting started with java challenge 3
im not sure how to do it, I have tried several times! - Print out to the screen using the printf method on console, "Firstname:" and the users first name! Ensure you have put the string formatter %s
// I have imported java.io.Console for you. It is a variable called console.
String firstName = console.readLine("What is your name? ");
String lastName = console.readLine("What is your name? ");
console.printf( );
Jonathan Petersen
45,721 PointsHey Stephen you don't normally use printf like that, if you wanted to print without formatting you could use
System.out.prinltn(firstName);
Also you don't concatenate string in Java with a period , you use + operator. This is different than some languages such as PHP. BTW, if you are going to be doing a lot of concatenation you should use Stringbuilder. I know it does not matter now, but that is something to keep in the back of your mind as you begin learning java.
The code you posted would result in a runtime uncompilable source code exception.
1 Answer
Jonathan Petersen
45,721 PointsHey I think you are supposed to print the first name to the console, then the last name.
Here is how you would print the string.
console.printf("First Name: %s", firstName);
The %s is the format specifier, in this case it means a string. The second argument is the string that should be printed in place of the format specifier.
Next you will need to make the necessary changes to output the last name.
Siddhant Dahiya
156 PointsThank to you so much jonathan :) it worked
Stephen Little
8,312 PointsStephen Little
8,312 PointsNot sure what your trying to do here, I have not yet started the java course but... if your trying to print out the fist and last name should they not be in your printf() fuction? something like
console.printf(firstName . lastName);
just guessing here with out seeing what your challenge is...
Hope I helped in some small way ;)
Stephen