java无法取消引用int_“不能取消引用double” [Java lang.]

这个问题已经在这里有了答案:            >            int cannot be dereferenced                                    3个

我不断收到错误:

“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

result = -1;

else

result = 1;

return result;

}

}

解决方法:

Java不允许在原语上调用方法.在这种情况下,它是equals()方法主体中的price.equals(…)调用.价格是原始的,因为它是两倍,并且是“dereference” basically means the method call.

标签:compareto,arrays,java

来源: https://codeday.me/bug/20191120/2045874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值