Can't seem to get .doubleValue () to work (I have latest Java version)

So, I am having a problem with a method I created called averageCost (). The error I am getting:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method doubleValue() is undefined for the type Double

at queue.averageCost(queue.java:38)
at queue.main(queue.java:47)

      

And earlier I tried to just add a double value to the Double object which obviously didn't work. So I found the .doubleValue method, but I can't seem to get it to work. I've added all my code (everything else works just fine) if it doesn't clear anything. I've been stuck on this for a couple of days while working on another, please help):

    import java.util.LinkedList;
    public class queue<Double> {

private LinkedList<Double> linklist;
private String symbol;
private String name;
private int shares;
private Double price;

public queue(String symbol2, String name2, int shares2, Double price2) { //so far, I have a linkedlist that works
    linklist = new LinkedList<Double>();
    shares = shares2;
    price = price2;
    symbol = symbol2;
    name = name2;
    bigAdd(shares, price);
}

public void add(Double e) {
    linklist.add(e);
}

public Double take() {
    return linklist.poll();
}       

public void bigAdd (int shares2, Double price2) { 
    while(shares2>0) {
        linklist.add(price2);
        shares2--;
    }
}

public double averageCost(int shares2) {
    double average = 0;
    int sizer = 0;
    while(sizer < shares2) {
        average = average + linklist.poll().doubleValue(); //this is where the problem lies
        sizer++;
    }
    average = average/shares2;
    return average;
}
    }

      

+3


source to share


2 answers


Your error comes from the class declaration.

The generic formal parameter can be T:

public class queue<T> {

      



Now you can see that T does not have a method named doubleValue because you did not restrict it.

Then follow these steps:

import java.util.LinkedList;

public class queue<T extends Number> {

   private final LinkedList<T> linklist;
   private final int           shares;
   private final T             price;

   public queue( int shares2, T price2 ) {
      linklist = new LinkedList<>();
      shares   = shares2;
      price    = price2;
      bigAdd( shares, price );
   }

   public void add( T e ) {
      linklist.add(e);
   }

   public T take() {
      return linklist.poll();
   }

   public void bigAdd( int shares2, T price2 ) {
      while(shares2>0) {
         linklist.add(price2);
         shares2--;
      }
   }

   public double averageCost( int shares2 ) {
      double average = 0;
      int sizer = 0;
      while( sizer < shares2 ) {
         final T v = linklist.poll();
         average = average + v.doubleValue();
         sizer++;
      }
      average = average/shares2;
      return average;
   }
}

      

+3


source


You can just use linklist.poll()

. Double

will get unboxed before Double

.



-1


source







All Articles