【问题标题】:Printf formatting error incompatible typesprintf格式错误不兼容类型
【发布时间】:2017-09-22 02:57:06
【问题描述】:

我不断收到此错误,提示类型不兼容:java.io.PrintStream cannot be converted to java.lang.String,但我不知道如何让它适用于我的 toString 方法。我已经尝试将它分配给一个变量然后返回它,然后只是计划将它作为返回语句打印出来,我看不出我的 printf 格式有什么问题。帮助表示赞赏。

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

【问题讨论】:

  • 您正在尝试将双精度打印为浮点数,使用d 进行双精度,f 用于浮点数

标签: java printf bluej incompatibletypeerror


【解决方案1】:
 return System.out.printf("%-15s $%-8.2f %-11d $%-8.2f", name, price, 
     quantity, price*quantity);

您正在尝试返回PrintStream。这是不正确的,因为 toString 应该返回一个字符串。

你应该使用String#format 方法,它在格式化后返回一个字符串

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

【讨论】:

    【解决方案2】:

    System.out.printf 不返回字符串,你正在寻找String.format

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

    【讨论】:

      【解决方案3】:

      改变

       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);
      

      因为System.out.println() 如果用于将字符串打印到控制台。不要创建格式化字符串-

      【讨论】:

        【解决方案4】:

        错误是由

        引起的
        return System.out.printf(....)
        

        如果你真的想从这个方法返回一个字符串,那么试试

        return String.format(....);
        

        【讨论】:

        • @dawood-ibn-kareem 感谢您的编辑。你结婚改名了吗?
        • 最近没有。只是我的句柄。
        猜你喜欢
        • 2013-09-23
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-11
        相关资源
        最近更新 更多