简体   繁体   中英

Why does this code not return a string?

public String toString()
    {
        System.out.println(lastName + ", " + firstName);
        System.out.println(phoneNumber);
        if (relationship == 'F')
            System.out.println("Friend");
        else if (relationship == 'M')
            System.out.println("Family Member");
        else if (relationship == 'B')
            System.out.println("Buisness Associate");
        else System.out.println("Not Specified");
    }

This does not work because it says the code does not return a string, and I don't know why.

Because you're not using return <variable> statement in your code. Printing in the System.out stream IS NOT the same as returning a String .

Instead printing the desired content of the String to output, it would be better to append all of it into a String and return this String to the client.

Here's an example:

public String toString() {
    StringBuilder result = new StringBuilder(lastName)
        .append(", ")
        .append(firstName)
        .append('\n')
        .append(phoneNumber);
    if (relationship == 'F')
        result.append(" Friend");
    else if (relationship == 'M')
        result.append(" Family Member");
    else if (relationship == 'B')
        result.append(" Business Associate");
    else result.append(" Not Specified");
    return result.toString();
}

Also, try that the toString method returns a way to understand the data stored in your object. This is another example:

public String toString() {
    StringBuilder result = new StringBuilder("Name: ")
        .append(lastName)
        .append(", ")
        .append(firstName)
        .append(". Phone Number: ")
        .append(phoneNumber)
        .append(". Relationship: ");
    if (relationship == 'F')
        result.append(" Friend");
    else if (relationship == 'M')
        result.append(" Family Member");
    else if (relationship == 'B')
        result.append(" Business Associate");
    else result.append(" Not Specified");
    return result.toString();
}

System.out.println仅打印到控制台,它不返回对象。

Try to return string like below:

public String toString()
{
    StringBuilder output = new StringBuilder(lastName + ", " + firstName);
    output.append(phoneNumber);
    if (relationship == 'F')
        return output.append("Friend").toString();
    else if (relationship == 'M')
        return output.append("Family Member").toString();
    else if (relationship == 'B')
        return  output.append("Buisness Associate").toString();
    else return  output.append("Not Specified").toString();
}

There is no return statement. Instead of printing the properties you should be building a string to return (improving the formatting of the returned String is left as an exercise for the reader):

public String toString() {
    StringBuilder s = new StringBuilder();
    s.append(lastName).append(", ").append(firstName);
    s.append(phoneNumber);
    if (relationship == 'F')
        s.append("Friend");
    else if (relationship == 'M')
        s.append("Family Member");
    else if (relationship == 'B')
        s.append("Buisness Associate");
    else s.append("Not Specified");

    return s.toString();
}

Normally, when you would use a "toString()" function, it would be used like this

// replace "Person" with what your class is actually named
String personString = Person.toString(); 
System.out.println(personString);

So, instead of actually printing to the console in your application, you need to tell the toString() function, what value it needs to return.

You use the return keyword to do that


Try this

public String toString()
{
    String result = lastName + ", " + firstName;
    // feel free to change `result` more
    return result;
}

Take not of the method signature

public String toString()

that String in the signature is what's telling the compiler that the method is supposed to return a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM