1

I keep getting this error saying incompatible types: java.io.PrintStream cannot be converted to java.lang.String and I don't get how to get it to work for my toString Method. I've tried assigning it to a variable and then returning it, and then just plan printing it out as a return statement, I don't see anything wrong with my printf formatting. Help is appreciated.

 import java.text.NumberFormat;

    public class Item
    {
        private String name;
        private double price;
        private int quantity;


        // -------------------------------------------------------
        //  Create a new item with the given attributes.
        // -------------------------------------------------------
        public Item (String itemName, double itemPrice, int numPurchased)
        {
        name = itemName;
        price = itemPrice;
        quantity = numPurchased;
        }


        // -------------------------------------------------------
        //   Return a string with the information about the item
        // -------------------------------------------------------
        public String toString ()
        {

        return System.out.printf("%-15s $%-8.2f %-11d $%-8.2f", name, price, 
     quantity, price*quantity);
        }

        // -------------------------------------------------
        //   Returns the unit price of the item
        // -------------------------------------------------
        public double getPrice()
        {
        return price;
        }

        // -------------------------------------------------
        //   Returns the name of the item
        // -------------------------------------------------
        public String getName()
        {
        return name;
        }

        // -------------------------------------------------
        //   Returns the quantity of the item
        // -------------------------------------------------
        public int getQuantity()
        {
        return quantity;
        }
    }  
1
  • You are trying to print a double as a float, use d for doubles, f for floats Commented Apr 25, 2017 at 8:00

4 Answers 4

1
 return System.out.printf("%-15s $%-8.2f %-11d $%-8.2f", name, price, 
     quantity, price*quantity);

You are trying to return PrintStream. That is not correct caause the toString should return a String.

You should have used String#format method, which gives back a String after fomratting

return String.format("%-15s $%-8.2f %-11d $%-8.2f", name, price, 
         quantity, price*quantity);
Sign up to request clarification or add additional context in comments.

Comments

1

System.out.printf does not return a string, You are looking for String.format

public String toString () {
    return String.format("%-15s $%-8.2f %-11d $%-8.2f", name, price, quantity, price*quantity);
}

Comments

1

Change

 System.out.printf("%-15s $%-8.2f %-11d $%-8.2f", name, price, 
 quantity, price*quantity);

to

 String.format("%-15s $%-8.2f %-11d $%-8.2f", name, price, 
 quantity, price*quantity);

Because System.out.println() if for printing a string to console. Not to create a formated string-

Comments

1

The error is caused by

return System.out.printf(....)

If you really want to return a String from this method then try

return String.format(....);

2 Comments

@dawood-ibn-kareem Thanks for the edit. Did you get married and change your name?
Not recently. Just my handle here.

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.