【发布时间】:2015-04-25 14:46:22
【问题描述】:
我不断收到错误:
布尔行中的“double can not be dereferenced”(第45行)
我从未见过这个错误。 inputdata.txt 是一个包含 8 个 Item 类输入的文本文件。我想知道我的代码有什么问题以及我应该如何解决它。
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;
}
}
【问题讨论】: