Ich bekomme diesen Fehler sagen inkompatible Typen: java.io.PrintStream cannot be converted to java.lang.String
und ich bekomme nicht, wie es für meine ToString-Methode zu arbeiten. Ich habe versucht, es zu einer Variablen zuzuweisen und es dann zurückzugeben, und dann plane, es als eine Rückkehranweisung auszugeben, sehe ich nichts falsch mit meiner printf-Formatierung. Hilfe wird geschätzt.Printf Formatierungsfehler inkompatible Typen
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;
}
}
Sie versuchen, eine Doppel zu drucken, wie ein float, benutze 'd' für doubles,' f' für floats – Ferrybig