Objects first with java - String length method
807601Mar 19 2008 — edited Mar 19 2008I 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?