3

I keep getting the error:

"double can not be dereferenced" in the boolean line (line 45)

I have never seen this error. inputdata.txt is a text file containing 8 Item class inputs. I would like to know what is wrong with my code and how I should go around fixing it.

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;
    }

}
nempoBu4
  • 6,521
  • 8
  • 35
  • 40
Spartan058
  • 31
  • 3

3 Answers3

2

Java does not permit calling methods on primitives. In this case it's the price.equals(...) call in the equals() method body. price is primitive because it's a double, and "dereference" basically means the method call.

Use == or a lenient comparison method.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

There is no such method equals() for primitives. use == for comparison.

Here price is double which is primitive.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

You declared the variable price as double (primitive)

public double price;

So you cannot call any method on it:

price.equals(((Item)other).getPrice()
Marcelo Keiti
  • 1,200
  • 9
  • 10