Need a fresh set of eyes

Hello!
I keep getting this error:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from double to Problem3.Point

    at Problem3.main(Problem3.java:18)

I’m not sure exactly what it means, and I think I just need another person to take a look at my code. I’m getting kind of lost looking at it myself. Thank you.
Here is my code:


import java.util.*;

public class Problem3 {

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter x1, y1, x2, y2, x3, y3: ");

        Point p1 = new Point(p1.x,p1.y);

        Point p2 = new Point(p2.x,p2.y);

        Point p3 = new Point(p3.x,p3.y);

        p1.x = sc.nextDouble();

        p1.y = sc.nextDouble();

        p2.x = sc.nextDouble();

        p2.y = sc.nextDouble();

        p3.x = sc.nextDouble();

        p3.y = sc.nextDouble();

        //call method

        Point area = getTriangleArea(p1, p2, p3);

        System.out.println("The area of the triangle is: "+ area);

        

    }

    public static double getTriangleArea(Point p1, Point p2, Point p3){

        double area = ((p1.x*(p2.y - p3.y)) + (p2.x*(p3.y - p1.y)) + (p3.x*(p1.y - p2.y)))/2;

        if(area == 0){

            System.out.println("This is not a triangle.");

            return area;

        }

        else{

            return area;

        }

        

    }

    class Point{

        double x;

        double y;

        public Point(double x, double y){

            this.x = x;

            this.y = y;

        }

    }

    

}

What your error is telling you is that you’re trying to convert a double to a Point, which can’t be done.

There. Your getTriangleArea() method returns a double value, and you’re trying to assign that value to a Point variable.

I believe you don’t even need your area variable to be of type Point…? So maybe if you just make area be a double instead of a Point that will do.

2 Likes

In addition to what @yizuhi pointed out, the above won’t work. You can’t pass values as arguments to a method before they are assigned. Things have to be done in order. First you need to get the user input for the 6 values (x1, x2, x3, y1, y2, y3), then you can create your Point instances using the user input values as arguments.

OR
You could create the Point instances first using default values of perhaps 0, and then re-assign the x and y properties using the user input values. For example:

Point p1 = new Point(0, 0); //supply 0's as the values for x and y initially
//then reassign x and y to the user input values
p1.x = sc.nextDouble();
p1.y = sc.nextDouble();
//and so on

Also, you attempted to format your code using 3 single quotation marks '''. Those won’t work. You need to use back ticks also called back quotes. They slant down from left to right. On my keyboard it’s right below the esc key. It looks like this: `  not like this: '

```
code goes here
```
2 Likes