Syntax Quiz 2, Step 10

Assignment:

Write a statement to print the words "The parameter is ", followed by the value of the variable theParameter.

My solution:

System.out.println ("The parameter is \"" + theParameter + "\".");

Common mistakes:

Putting a period after println.
"println" is a method name, not an object, so it must be followed by a pair of parentheses surrounding its argument(s).
Putting theParameter in quotation marks.
If you print out "theParameter", that's exactly what you'll see: the letter "t", then "h", then "e", then "P", then "a", etc. The assignment was to print out the value of the variable theParameter, not its name.
Omitting the semicolon.
System.out.println(...) is a statement, and therefore must end with a semicolon.
Omitting System.out.
When you call a method without an object in front, as in
println("The parameter is \"" + theParameter + "\".");
you're really saying this.println(...) However, this is an instance of the QuizClass class, which doesn't have a println method, so the statement won't compile. System.out is a predefined object of a class which does have a method named println.
Omitting the plus signs and/or using commas there.
The println method is defined to take in one String parameter. It cannot take two or three, so if you have several strings to print on the same line, the usual tactic is to concatenate them using + or concat(...).
What's with the backslash-quotation marks?
The assignment didn't specifically say that the parameter should be printed out enclosed in quotation marks, but if you want to do so, remember to "escape" them by preceding them with back-slashes. If you don't, and write something like
System.out.println ("The parameter is "" + theParameter + "".");
the Java compiler will think the first string is "The parameter is ", followed immediately (with no operators in between) by the string " + theParameter + ", followed immediately again by the string ".". It will probably produce the error message ") or , expected", because when one string argument to a method is over, it expects the next character to be either a comma (before the next argument) or a right parenthesis (closing the argument list).

Stephen Bloch / sbloch@adelphi.edu