Je continue d'obtenir cette erreur en disant incompatibles types: java.io.PrintStream cannot be converted to java.lang.String
et je ne comprends pas comment le faire fonctionner pour ma méthode toString. J'ai essayé de l'assigner à une variable puis de la renvoyer, puis de l'imprimer en guise de déclaration, je ne vois rien de mal dans le formatage de mon printf. L'aide est appréciée.Erreur de formatage Printf types incompatibles
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;
}
}
Vous essayez d'imprimer un double comme un flotteur, utilisez 'd' pour les doubles,' f' pour les flotteurs – Ferrybig