0

I am getting this error: "this method must return a result of type string" for this code. Also, I haven't been able to find much about the .printf method (I just got it from my textbook since I needed to format this exactly like they had in there). Was this method removed?

public String toString()
{
    System.out.println("Shown are the prices for " +
            "each seat. A 0 indicates the seat is " +
            "unavailable.");
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            System.out.printf("%8d", seatArray[i][j]);
        }
    }
}
1
  • the error says it all. Return String Commented Nov 13, 2013 at 7:54

3 Answers 3

1
public String toString() // returns a String

The return type of this method is a String, but you don't have a return statement at all. You need to have a return statement, which returns a String from this method. There is no problem with using a System.out.printf.

public String toString()
{
    System.out.println("Shown are the prices for " +
            "each seat. A 0 indicates the seat is " +
            "unavailable.");
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            System.out.printf("%8d", seatArray[i][j]);
        }
    }
    return someString; // return statement is required as the method return type is String and not void
}

Note that, you can't change the return type to void, because when you use the toString() name for your method, it is overriding the toString() method of the Object class, whose return type is String. You can either rename the method and make it void, or if you wish to keep the same name(and override the Object.toString()), then you need to have a return statement for sure.

public void printArray() // Something like this

And yeah, whether the return type is void or String, you can always print your array in the fashion you've done. Just that if its a String return type, you'll have to return a String along with the printing you did in the method.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you again, RJ. So is there a way to still print it like that from this method? I switched it to public void, but then it gave this error "The return type is incompatible with Object.toString()"
@user1953907 If you are using a void method, don't name it toString(), name it something like print() or display().
@user1953907 - I edited my answer to answer to your doubt. Let me know if you need more clarity on it! :)
1

You need to return a String as per your method signature. Try this.

public String toString()
{
    StringBuilder sb = new StringBuilder()'
    sb.append("Shown are the prices for " +
           "each seat. A 0 indicates the seat is " +
           "unavailable.\n");

    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            sb.append(String.format("%8d\n", seatArray[i][j]));
        }
    }

    return sb.toString();
}

Comments

0

No, PrintStream.printf() was not removed. System.out is an instance of java.io.PrintStream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.