Reputation: 26009
I'm not sure what's going on here but here's a section I'm having problems with (there's a few others but I think if I can figure out the logic here I can apply it elsewhere).
I recently had a problem where I was using point but couldn't get decimals so, as suggested, I switched to point2D to allow for doubles. Since switching I have been getting errors that I can't seem to understand how to fix because I don't see the cause (I know changing to point2D triggered it but I can't see the connection).
I have the following code:
Double x_data = (Double) data.get(k);
for (int c = 0; c <= 100; c++) { //the c variable caps the percent
double percent = roundTwoDecimals(c*.01);
double value1 =(sum - percent * x_data);
data is a list of point2D(which I'm casting to to double and sum is a variable I send in to this method that is really an integer but I cast it as a double. In eclipse I get the dreaded red underline under 'percent * x_data'. I googled and saw this error is often caused by someone trying to multiply a string or something but I'm fairly certain these all doubles. I get the following error now:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The operator * is undefined for the argument type(s) double, Point2D.Double
The operator * is undefined for the argument type(s) double, Point2D.Double
Syntax error, insert ")" to complete ArgumentList
The operator * is undefined for the argument type(s) double, Point2D.Double
As far as I can tell everything involved in this process is either double or casted to double so I'm not sure why Point2D is involved in these operators.
Also I had a method that limited decimal places:
static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.###");
return Double.valueOf(twoDForm.format(d));
but Double.valueOf is not one of the options anymore so I got errors (Point2D errors) on that(to get around it, I just return d back right now without limiting the decimals).
I don't think there is a problem with point2D or anything but I must be structurally doing something wrong.
Upvotes: 1
Views: 10258
Reputation: 328669
According to the stack trace, x_data is of type Point2D.Double
, not java.lang.Double
.
Check your imports, remove Point2D.Double
and add java.lang.Double
.
Now you won't be able to cast a Point2D
to a Double
. You probably mean to use yourDoublePoint2D.getX()
adn yourDoublePoint2D.getY()
which return double
s (no cast required).
As a side note, you can write something like this:
Double d1 = 1.5;
double d2 = 2.3;
double d3 = d1 * d2;
EDIT
You are likely importing Point2D.Double
class with this:
import java.awt.geom.Point2D.*;
or
import java.awt.geom.Point2D.Double;
That creates a name collision with java.lang.Double
. See below an example of code that would work as you expect:
import java.awt.geom.Point2D; //Note: only import Point2D, not Point2D.Double
public class Test {
public static void main(String[] args) {
Point2D.Double point = new Point2D.Double(1.5, 2.5);
double x = point.getX(); //1.5
double y = point.getY(); //2.5
Double xx = point.getX(); //1.5
Double yy = point.getY(); //2.5
}
}
Upvotes: 3
Reputation: 1477
If you don't import Point2D.Double, but refer to all instances of it by the full package name, an "unadorned" Double should be java.lang.Double.
Upvotes: 0