首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“双不能取消引用”[Java .]

“双不能取消引用”[Java .]
EN

Stack Overflow用户
提问于 2015-02-24 04:09:17
回答 3查看 107关注 0票数 3

我不断地发现错误:

布尔行(第45行)中的"double不能取消引用“

我从未见过这样的错误。inputdata.txt是一个包含8个条目类输入的文本文件。我想知道我的代码出了什么问题,以及我应该如何修复它。

代码语言:javascript
运行
复制
import java.util.Scanner;
import java.io.*;
public class Item implements Comparable
{

    public String item, category;
    public int quantity; 
    public double price;

    public Item(String itemName, String category, int quantity, double price)
    {
       this.item = item;
       this.category = category;
       this.quantity = quantity;
       this.price = price;
    }

    private Item[] list = new Item[8];
    private int n=0;

    public Item input() throws IOException
    {
        Item oneItem;
        Scanner scan = new Scanner ( new File ("Inputdata.txt"));
        while (scan.hasNext())
        {
           oneItem = new Item(scan.next(), scan.next(), scan.nextInt(), scan.nextDouble() );
           list[n] = oneItem;
           n++;
        }
    }

     public String toString()
    {
        String s = "";
        s += "[Clothing Name:" + item + ", Category: " + quantity + ", Price: " + price;
        return s;
    }

    public String getCategory()
    {
        return category;
    }

    public boolean equals (Object other)
    {
        return(price.equals(((Item)other).getPrice()) &&
        category.equals(((Item)other).getCategory()));
    }

     public int compareTo (Object other)
    {
        int result;

        double otherPrice = ((Item)other).getPrice();
        String otherCategory = ((Item)other).getCategory();

        if (price == otherPrice)
            result = price.compareTo(otherPrice);
        else if (price <otherPrice)
            result = -1;
        else 
            result = 1;

        return result;
    }

}
EN

回答 3

Stack Overflow用户

发布于 2015-02-24 04:10:54

Java不允许对原语调用方法。在本例中,它是equals()方法体中的equals()调用。price是原语,因为它是一个double,而"dereference“基本上是指方法调用。

使用==比较宽松的方法。

票数 2
EN

Stack Overflow用户

发布于 2015-02-24 04:12:04

没有这样的方法equals()用于primitives。使用==进行比较。

这里的价格是double,也就是primitive

票数 1
EN

Stack Overflow用户

发布于 2015-02-24 04:15:54

您将可变价格声明为双(原语)。

代码语言:javascript
运行
复制
public double price;

因此,您不能在上面调用任何方法:

代码语言:javascript
运行
复制
price.equals(((Item)other).getPrice()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28687995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档