Error in main method

I’ve been getting this error in both programs I’ve written today:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:

    at Problem3.main(Problem3.java:4)

Except there doesn’t seem to actually be anything wrong with my code. Software/Java is up to date, this week is the most often I’ve gotten this error since I started coding in August. Please help.

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: ");

        int x1 = sc.nextInt();

        int y1 = sc.nextInt();

        int x2 = sc.nextInt();

        int y2 = sc.nextInt();

        int x3 = sc.nextInt();

        int y3 = sc.nextInt();

        Point p1 = new Point(x1, y1);

        Point p2 = new Point(x2, y2);

        Point p3 = new Point(x3, y3);

        getTriangleArea(p1, p2, p3);

        

    }

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

        double area = ((x1*(y2 - y3)) + (x2*(y3 - y1)) + (x3*(y1 - y2)))/2;

        if(area == 0){

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

        }

        else{

            return area;

        }

    }

    

}

public class Point{

    double = x;

    double = y;

    public Point(double x, double y){

        this.x = x;

        this.y = y;

    

}

I’m not sure if this will solve the problem, but I think you’re missing a } to close your Point class block. You have one to start the class block, then one to start the constructor method block, then one to close the constructor method block and that’s it. You never really closed the Point class block.

Hope this helped :slightly_smiling_face:

Edit: I just double-checked your code and I found that you have some other errors…

There you’re missing a semicolon

That doesn’t look right…what are you trying to do here? If you wanted to declare two double variables, x and y, without assigning them any values yet, this would be the right way:

double x;
double y;
2 Likes

Adding to what I said before…

It’s been quite some time since I last used Java, but I believe you can’t have two class declarations in one file. You should have a Problem3.java file where you define the Problem3 class and a Point.java file where you define the Point class.

2 Likes

Adding to what @yizuhi said, you could also make Point a public static class, and include it in your Problem3 class. You have some issues with your getTriangleArea method as well. You take 3 Point instances as parameters, but you use undefined variables (x1, y1, x2, y2, x3, y3) to calculate the area. Also, all paths have to return a double value, so you need to return a double after printing your message, "This is not a triangle."

Hint:

Each of your Point instances has an x property and a y property. How do you access those?

Further Hint:

You can access the properties of a class instance with dot notation:

p1.x
//or
p1.y
//etc.
2 Likes

Ok everyone, I took in your suggestions along with some help from my professor. I got through my initial problem but I now have a new set of errors.
Here are the errors:

Exception in thread “main” java.lang.Error: Unresolved compilation problems:
The constructor Problem3.Point() is undefined
p2 cannot be resolved to a variable
p2 cannot be resolved to a variable
p3 cannot be resolved to a variable
p3 cannot be resolved to a variable
p2 cannot be resolved to a variable
p3 cannot be resolved to a variable

    at Problem3.main(Problem3.java:7)

And 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();

        Point p2 = new Point();

        Point p3 = new Point();

        p1.x = sc.nextDouble();

        p1.y = sc.nextDouble();

        p2.x = sc.nextDouble();

        p2.y = sc.nextDouble();

        p3.x = sc.nextDouble();

        p3.y = sc.nextDouble();

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

        

    }

    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;

        }

    }

    

}

Your error message references line 7:

Your Point class has a constructor with 2 parameters. To use it, you must supply 2 arguments.

You did so originally:

Also, when you paste code, make sure to place it between lines with only 3 back ticks or back quotes on them. If you click the </> button, you can paste your code in the space indicated, or you can select all of your code after pasting, and then click </>, or just type the back ticks yourself:

```
code goes here
```

Following this practice will make your code appear as it does now since I edited your post. :slightly_smiling_face:

2 Likes