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 trialBen Cotton
Courses Plus Student 105 PointsNot sure where I'm going wrong, I thought I had to stipulate the variable's name within the brackets (or parentheses)?
It's giving me an error but I'm not sure where I've gone wrong. I'm wondering if I've made a syntax error, but since I'm very new to Java I'm not easily able to diagnose the problem.
Thank you in advance.
// 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 surname? ");
console.printf= ("First name: %s."); firstName;
All solved now :D
I'm not sure if there's a way to change the status of this thread to 'resolved'?
3 Answers
Ben Cotton
Courses Plus Student 105 PointsWait I think I've figured it. I think it should be "console.printf(" rather than "console.printf=".
Ian Holden
8,556 PointsOk, I am not an expert with this method but I have noticed an issue with your final line of code, so I hope this helps in some way.
This line: console.printf= ("First name: %s."); firstName;
will print the string that you have written: "First name: %s.". However, I can see that you want to print the firstName variable which at the moment is sitting on its own as an individual statement. It is an individual statement at the moment because you have a semi-colon at the end of the closing brackets after your string in the line above. This means that you have something that in the compiler will be read like this:
console.printf= ("First name: %s."); firstName;
Now you can see that you actually have two statements, the first that may print your string and the second which will do nothing. I think the second line could be causing your error! Currently, the variable is just being named but not used in the statement which makes it invalid.
The next few lines are just a guess but could you try using the firstName variable in the first line statement like so:
console.printf= ("First name: %s.", firstName);
See how this goes and let me know :) I hope this worked/helped.
Ben Cotton
Courses Plus Student 105 PointsThanks for the response, Ian :D
I think I messed up the last line trying to find a solution to my problem (which was the '( or =' conundrum above) and therefore caused myself more hassle haha!
All resolved now, though, lessons learnt.
Thanks for the help :)
Johan Prieto
6,513 PointsYeah Ben, it has to be console.printf("") like you said :D
Ben Cotton
Courses Plus Student 105 PointsThanks very much for the response, Johan. I got it working :D
Ian Holden
8,556 PointsIan Holden
8,556 PointsAh yes! I missed this in my answer haha! That certainly will be an issue too.