5

I am working on a really simple point class but I am getting an error and I can't pinpoint where the String/double problem is happening or how to fix it.

public String getDistance (double x1,double x2,double y1,double y2) {

            double X= Math.pow((x2-x1),2); 
            double Y= Math.pow((y2-y1),2); 

            double distance = Math.sqrt(X + Y); 
            DecimalFormat df = new DecimalFormat("#.#####");

            String pointsDistance = (""+ distance);

             pointsDistance= df.format(pointsDistance);

            return pointsDistance;
        }

and the test code

double x1=p1.getX(),
                       x2=p2.getX(), 
                       y1=p1.getY(),
                       y2=p2.getY(); 

           pointsDistance= p1.getDistance(x1,x2,y1,y2);

EDIT

I forgot to add the error I'm receiving:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at Point.getDistance(Point.java:41)
at PointTest.main(PointTest.java:35)
rgettman
  • 176,041
  • 30
  • 275
  • 357
user2954611
  • 57
  • 1
  • 1
  • 3
  • 1
    Fortunately, you don't have to pinpoint the error since your compiler will do that for you... Where is `p1`, `p2`, and `pointsDistance` defined? What type is `p1` and `p2`? I assume `pointsDistance` is a `String`. What does `getX()` and `getY()` return on whatever object type `p1` and `p2` are? Which line is `Point.java, line 41`? Which is `PointTest.java line 35`? – nhgrif Nov 05 '13 at 01:04
  • pointsDistance= p1.getDistance(x1,x2,y1,y2); is line 35 – user2954611 Nov 05 '13 at 01:08
  • pointsDistance= df.format(pointsDistance); is line 41 – user2954611 Nov 05 '13 at 01:08
  • So, there are two posted answers that explain that answer, but without the problem being obvious to me (I'm not a Java guru), the compiler has given me enough information to know that I should probably do some research on the `DecimalFormat` class and know what kind of arguments the `format` method expects. After all, the error is `ILLEGAL ARGUMENT EXCEPTION` and points you straight to this line. – nhgrif Nov 05 '13 at 01:10

5 Answers5

3

You passed a String, but the format method expects a double and returns a String. Change from

String pointsDistance = (""+ distance);
pointsDistance= df.format(pointsDistance);

to

String pointsDistance = df.format(distance);
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

Replace this:

String pointsDistance = (""+ distance);

pointsDistance= df.format(pointsDistance);

with:

String pointsDistance = df.format(distance);

The problem is that your number format doesn't accept a string.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • @user2954611 - Did you change the argument to `df.format()`? It is important to pass a `double`, not a `String`. – Ted Hopp Nov 05 '13 at 01:19
1

The problem is that the format method takes a numeric value, not a String. Try the following:

public String getDistance(double x1, double x2, double y1, double y2) {
    double X = Math.pow((x2-x1), 2); 
    double Y = Math.pow((y2-y1), 2); 

    double distance = Math.sqrt(X + Y); 
    DecimalFormat df = new DecimalFormat("#.#####");

    String pointsDistance = df.format(distance);
    return pointsDistance;
}
blacktide
  • 10,654
  • 8
  • 33
  • 53
1

Use

String pointsDistance = df.format(distance);

as the format method expects a double and not a string.

Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
1

Check out here first:
http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html#format(double,%20java.lang.StringBuffer,%20java.text.FieldPosition)
Does your use of format method same as that?

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55