Search code examples
javacomparator

How to fix "java: cannot find symbol"


I'm using JB IntelliJ IDEA and trying to create a program which sorts Rectangles. But I can't fix

"Error:(28, 22) java: cannot find symbol
  symbol:   method Rectangle(java.lang.Double,java.lang.Double)
  location: class io.github.vadimsam.rectsort.Rectangle".

What should I do?

NB: recreation of project didn't help me.

Main:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

class SComparator implements Comparator<Rectangle> {

    public int compare(Rectangle r1, Rectangle r2) {
        return r1.area().compareTo(r2.area());

    }
}

public class Main {

    public static void main(String[] args) {

       ArrayList<Rectangle> rectsArea = new ArrayList<>();
       Scanner input = new Scanner(System.in);
       SComparator comparator = new SComparator();

       while(true){
            System.out.print("A = ");
            Double a = input.nextDouble();
            System.out.print("B = ");
            Double b = input.nextDouble();
            Rectangle.Rectangle(a,b);
            rectsArea.sort(comparator);
            if (a  == 0) {
                System.out.println("Sorted ArrayList:");
                System.out.println(rectsArea);
                break;
            }
        }
    }
}

Rectangle:

public class Rectangle {
    public Double c, d;

    public Rectangle(Double c, Double d) {
        this.c = c;
        this.d = d;
    }

    Double area() {
        return c * d;
    }
}

It should return sorted ArrayList


Solution

  • Instead of writing:

    Rectangle.Rectangle(a,b);
    

    which is calling a static method named Rectangle, which is potentially part of Rectangle class, try to create an object using constructor that you declared in your Rectangle class to instantiate new rectangle object :

    Rectangle rectangle = new Rectangle(a, b);
    

    You have not declared any static method named Rectangle in your Rectangle class, that is why this error appears.