我不断地发现错误:
布尔行(第45行)中的"double不能取消引用“
我从未见过这样的错误。inputdata.txt是一个包含8个条目类输入的文本文件。我想知道我的代码出了什么问题,以及我应该如何修复它。
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;
}
}发布于 2015-02-24 04:10:54
Java不允许对原语调用方法。在本例中,它是equals()方法体中的equals()调用。price是原语,因为它是一个double,而"dereference“基本上是指方法调用。
使用==或比较宽松的方法。
发布于 2015-02-24 04:12:04
没有这样的方法equals()用于primitives。使用==进行比较。
这里的价格是double,也就是primitive。
发布于 2015-02-24 04:15:54
您将可变价格声明为双(原语)。
public double price;因此,您不能在上面调用任何方法:
price.equals(((Item)other).getPrice()https://stackoverflow.com/questions/28687995
复制相似问题