2017-04-25 3 views
1

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; 
     } 
    } 
+0

Vous essayez d'imprimer un double comme un flotteur, utilisez 'd' pour les doubles,' f' pour les flotteurs – Ferrybig

Répondre

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

Vous essayez de revenir PrintStream. Ce n'est pas correct car le toString devrait renvoyer une chaîne.

Vous devriez avoir utilisé la méthode String#format, ce qui rend une chaîne après fomratting

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

System.out.printf ne renvoie pas une chaîne, vous cherchez String.format

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

changement

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

à

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

Parce que System.out.println() si pour l'impression d'une chaîne à la console . Ne pas créer un formated string-

1

L'erreur est causée par

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

Si vous voulez vraiment renvoyer une chaîne de cette méthode essayez

return String.format(....); 
+1

@ dawood-ibn-kareem Merci pour l'édition. Tu t'es marié et tu as changé ton nom? –

+0

Pas récemment. Juste ma poignée ici. –