Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Objects first with java - String length method

807601Mar 19 2008 — edited Mar 19 2008
I have tried to do exercise 2.79 but to no avail. basically it states,

'Modify your printDetails method to include printing the reference number. However, the method should print the reference number only if it has been set - taht is, the refNumber string has a non-zero length. If it has not been set, then print the string "ZZZ" instead. Hint: Use a conditionla statement whose test calls the length method on the refNumber string.

So I tried this:

public void printDetails()
{
if(refNumber.length() > 0) {
System.out.println("Author: " + author + "," + " Title: " + title + "," " Pages: " + pages + "," + "RefNumber: " + refNumber);
}
else{
System.out.println("Author: " + author + "," + " Title: " + title + "," " Pages: " + pages + "," + "RefNumber: " + "ZZZ");
}
}


But it does not print "ZZZ". it only prints "RefNumber: " when the ref number has not been set. Can anyone help me with this please?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2008
Added on Mar 19 2008
8 comments
284 views