-3

I made a class Operator and a method called compare. However, when call this method in my program, I always got error message

InfixToPostfix.java:17: error: cannot find symbol System.out.println(compare(op,op1)); ^ symbol: method compare(Operator,Operator) location: class InfixToPostfix

I think I did not make any spell mistakes.

public class Operator extends Token {

    protected String val; //Modified by Qinjianhong Yang, 11/18/16

    public boolean isOperator() { return true; }
    public boolean isOperand() { return false; }

            // helper method, returns (assigns) precedence for operators
    protected int getPrec()
    {
            //modified by Qinjianhong Yang, 11/17/2016
            if(this.val.equals("+") || this.val.equals("-")){
                    return 1;
            }
            else return 2;

    }

            // handy for comparing 2 operators
    public static int compare( Operator a, Operator b )
    {
            if( a.getPrec() == b.getPrec() )
                    return 0;
            else if( a.getPrec() < b.getPrec() )
                    return -1;
            else
                    return 1;
    }

    public String getVal() { return this.val; }

    public Operator( String v ) { this.val = v; }

} 

I call this function like this:

 Operator op = new Operator("+");
 Operator op1 = new Operator("*");
 System.out.println(compare(op,op1));
  • 3
    Note that the error message says it's look in class `InfixToPostfix`, but your `compare` method is in class `Operator`. Do this instead: `System.out.println(Operator.compare(op,op1));` – Jesper Nov 21 '16 at 07:55
  • 1
    Unless you `import` the method, you have to say `Operator.compare(op,op1)`. – Thilo Nov 21 '16 at 07:55

3 Answers3

2

I call this function like this:

Operator op = new Operator("+");
Operator op1 = new Operator("*");
System.out.println(compare(op,op1));

The error message tells us that code is in the class InfixToPostfix, but using compare without any qualifier would only work

  • for code inside the Operator class, or

  • if you used a static import:

      import static Operator.compare;
    

Otherwise, you have to tell the compiler where compare comes from:

System.out.println(Operator.compare(op,op1));
// ----------------^^^^^^^^^

Side note: The name "compare" suggests you're comparing the Operator as a whole, but your compare only compares the operator's precedence, ignoring its val. So for instance, Operator.compare(new Operator("*"), new Operator("/")) would return 0, which is surprising. If it's only going to compare precedence, I would suggest calling it comparePrecedence or similar.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You have to use: System.out.println(Operator.compare(op,op1));. You must add the classname if you call the static function outside of it

Jens
  • 67,715
  • 15
  • 98
  • 113
0

compare(op,op1) method is static, so you need to add the class name Operator to invoke it as shown below:

System.out.println(Operator.compare(op,op1));
Vasu
  • 21,832
  • 11
  • 51
  • 67