0

Basically, say if I have two double values as such:

 import javax.swing.JSpinner;
 import javax.swing.SpinnerNumberModel;


public class UI  {
    public static void main(String args[]){
        JSpinner stonesSpinnerImperial;
        JSpinner poundsSpinnerImperial;

        stonesSpinnerImperial = new JSpinner();
        poundsSpinnerImperial = new JSpinner();

        stonesSpinnerImperial.setModel(new SpinnerNumberModel(3, 3, 31, 1));
        poundsSpinnerImperial.setModel(new SpinnerNumberModel(0.0, 0.0, 13.0, 1.0));

        double imperial_stones = (double) stonesSpinnerImperial.getModel().getValue();
        double imperial_pounds = (double) poundsSpinnerImperial.getModel().getValue();
        double total_imperial_weight =  imperial_stones + imperial_pounds;

    }
}

However I get the following error: java.lang.Integer cannot be cast to java.lang.Double

I feel my logic behind this is flawed and any assistance in this matter would be greatly appreciated.

EDIT: I've added as much as I feel is required.

Arsen Davtyan
  • 1,891
  • 8
  • 23
  • 40
Banned
  • 201
  • 6
  • 17

3 Answers3

3

Why not simply do:

  double imperial_stones = (Integer)stonesSpinnerImperial.getModel().getValue();
  double imperial_pounds = (Double) poundsSpinnerImperial.getModel().getValue();
  double total_imperial_weight = imperial_stones + imperial_pounds;

Note that you have to cast the object returned to a numeric wrapper class, not a primitive class, thus Double, not double, and Integer, not int.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • It causes a mismatch, Type mismatch: cannot convert from double to int, sorry had not realised your edit. – Banned Apr 30 '12 at 01:44
  • should I need to change the constructors to double as suggested by @trashgod ? If not what is the reasoning behind not doing it in that way? – Banned Apr 30 '12 at 01:58
  • 1
    @Banned: The constructors should use doubles if you want to display double numbers. If the numbers displayed will always be int values, then use ints in your constructor. – Hovercraft Full Of Eels Apr 30 '12 at 02:00
  • As you have mixed models, I'd [say](http://stackoverflow.com/a/10377606/230513) the parent, `Number`. +1 – trashgod Apr 30 '12 at 02:00
3

Use the SpinnerNumberModel that accepts double values; it uses Double internally. For example,

new SpinnerNumberModel(3d, 3d, 31d, 1d)

Addendum: Based on your updated code, consider casting to Number,

    double imperial_stones =
        ((Number)stonesSpinnerImperial.getModel().getValue()).doubleValue();
    double imperial_pounds =
        ((Number)poundsSpinnerImperial.getModel().getValue()).doubleValue();
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

If the dynamic type returned by stonesSpinnerImperial.getModel().getValue() is an Integer and you try to cast it to a Double, you'll get a ClassCastException.

java.lang.Integer cannot be cast to java.lang.Double

In your case, the dynamic type is an Integer since you're using the constructor that accepts integers. Use the constructor that accepts doubles or restrict your computations to integer arithmetic.

blackcompe
  • 3,180
  • 16
  • 27