gpt4 book ai didi

java - "double can not be dereferenced"[Java 语言]

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:04 25 4
gpt4 key购买 nike

<分区>

我不断收到错误:

"double can not be dereferenced" in the boolean line (line 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;
}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com