[Solved] unexpected type; required: variable; found: value


Change this line.

if(yearPublished=0&monthPublished=0){
   return ("Published: "+"invalid number");

to

if(yearPublished == 0 && monthPublished == 0){
    return ("Published: "+"invalid number");

I imagine you are now getting your second, unreachable return, error due to you having an else statement above this block of code, which is called when your conditions in your if-else if loops are false. The else portion will always execute if your conditions are false once it reaches it, thus making this block unreachable.

Here is one easy way to return the string that you want. Simply declare a string to return, then keep adding to it and finally return it once you’ve evaluated all of your conditions.

public String printDetails()
{
    String returnString = ""; //declaring a string to build upon to return once finished all conditions.

    if(title !=null)
    {
        returnString += "Title: " + title + " ";
    }
    else if(title == null || title.length() <= 3)
    {
        returnString += "Title: " + "invalid text ";
    }

    if(bookNumber >= 10000 && bookNumber <= 20000)
    {
        returnString += "ISBN: " + bookNumber + " ";
    }
    else if(bookNumber == 0)
    {
        returnString += "ISBN: " + "invalid number ";
    }

    if(lastName == null || firstName == null)
    {
        returnString += "Author: " + "invalid text ";                                
    }
    else if(firstName != null || lastName != null) // changed to not= null.
    {
        returnString += "Author: " + firstName + " " + lastName + " ";
    }

    if(yearPublished == 0 && monthPublished == 0)
    {
        returnString += "Published: " + "invalid number ";

    }
    else if(yearPublished != 0 && monthPublished != 0)
    {
        returnString += "Published: " + monthName + " " + yearPublished;
    }

    return returnString; // now after the string has been built, we will return it.
}

Finally:

Your setMonthName() method is perfect for a switch statement, The example given is actually exactly what you would want to do. You might also want to accept monthPublished as an argument instead of monthName.

10

solved unexpected type; required: variable; found: value