Welcome to the Treehouse Community
Looking to learn something new?
Schwartz PrinceCourses Plus Student 2,132 Points
Couldn't get the output. Please help
Please help me in finding out when I went wrong.
// I've imported java.io.Console for you. It is stored in a variable called console for you. String name = console.readLine("Schwartz"); String pastTenseVerb = console.readLine("Did "); console.printf("%s", name, "really %s",pastTenseVerb, "this coding exercise");
The problem is your
printf method call. When using
printf you have to pass a string to be formatted as the first argument (which can contain any number of placeholders) and then you provide the values for those placeholders as the further arguments. For example:
String example1 = "string1"; String example2 = "string2"; console.printf("The contents of example1 is %s, the contents of example2 is %s", example1, example2); // The above will print: "The contents of example1 is string1, the contents of example2 is string2, example1, example2"
You cannot alternate between providing strings with placeholders and variables to place in them like you have done.
Joseph Wasden20,406 Points
You are close. So, first, with the field declarations. Whenever we use console.readLine(), we need to remember that the bit that is in the paranthesis (in other words, the parameters) are used as a text prompt to have the user type in a value. That typed value becomes the value of our variable. So, we want to keep the string in the parameters of the readLine method descriptive of the variable the user will be setting.
Also, when using printf(), you need to declare your entire string upfront. we can use the %s placeholder multiple times, and the comma separated bits after the string will be used concurrent to the order of the %s placeholders.
See the code below.
// I've imported java.io.Console for you. It is stored in a variable called console for you. String name = console.readLine("Enter a name: "); String pastTenseVerb = console.readLine("Enter a past-tense verb: "); console.printf("%s really %s this coding excercise", name, pastTenseVerb);