maker1
maker1

Reputation: 21

Removed Switch statement to resolve error and error persists.

Here is my issue. I removed the switch statement from this method as I had to alter it to accommodate multiple variables whereas the switch statement only allows char constant. Could not use Strings for variables without java version 7. So what I have done is changed to usual if/else statements to go through. But when trying to run the program I am still getting an error on the switch statement as follows:

Any ideas? If would like me to include code from tester I can just ask.

java.lang.Error: Unresolved compilation problem: Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

at Project2.PostfixEvaluator.calculate(PostfixEvaluator.java:124) at Project2.PostfixEvaluator.eval(PostfixEvaluator.java:71) at Project2.ExpressionEvaluator.evaluate(ExpressionEvaluator.java:29) at Project2.ExpressionEvaluatorTester.main(ExpressionEvaluatorTester.java:32)

public Token calculate(Token opr, Token opd1, Token opd2)
    {
        // Get the first String from opr, it is the operator: +, -, ...
        String oper = opr.getBody();

        System.out.println(opr);

        //Get the two operands by converting from String to int
        int op1 = Integer.parseInt(opd1.getBody());
        int op2 = Integer.parseInt(opd2.getBody());

        //Default return value, in case an error occurs
        int res = 0;

        /**
         * Alterations begin here
         * Performs operation and sets value for res
         */

           if(oper.equals("+")) 
           {
               res = op1+op2;
           }
           else if(oper.equals("-"))
           {
               res = op1-op2;  
           }
           else if(oper.equals("*")){
               res = op1*op2;
           }
           else if(oper.equals("/") && op2 != 0){
               res = op1/op2;
           }
           else if(oper.equals("/") && op2 == 0){
               System.out.println("Division by zero error in"+
                 " PostfixEvaluator.calculate().");
           }
           else if(oper.equals("%") && op2 != 0){
                res = op1%op2;
           }
           else if(oper.equals("%") && op2 == 0){
                System.out.println("Division by zero error in"+
                 " PostfixEvaluator.calculate().");
           }
           else if(oper.equals("<") && (op1 < op2)){
                res = 1;
           }
           else if(oper.equals("<") && (op1 >= op2)){
                res = 0;
           }
           else if(oper.equals("<=") && (op1 <= op2)){
                res = 1;
           }
           else if(oper.equals("<=") && (op1 > op2)){
                res = 0;
           }
           else if(oper.equals(">") && (op1 > op2)){
                res = 1;
           }
           else if(oper.equals(">") && (op1 <= op2)){
                res = 0;
           }
           else if(oper.equals(">=") && (op1 >= op2)){
                res = 1;
           }
           else if(oper.equals(">=") && (op1 < op2)){
                res = 0;
           }
           else if(oper.equals("==") && (op1 == op2)){
                res = 1;
           }
           else if(oper.equals("==") && (op1 != op2)){
                res = 0;
           }
           else if(oper.equals("!=") && (op1 != op2)){
                res = 1;
           }
           else if(oper.equals("!=") && (op1 == op2)){
                res = 0;
           }
           else if(oper.equals("||") && true){
                res = 1;
           }
           else if(oper.equals("&&")&& true){
                res = 1;
           }
           else
               res = 0;





        //Convert res into a Token and return it.
        return new Token(""+res);

}

Upvotes: 0

Views: 663

Answers (2)

Yogendra Singh
Yogendra Singh

Reputation: 34387

Perform a clean build and then run your program:

Clean: Menu --> Project --> clean

Build: Menu --> Project --> Build Automatically

or Menu --> Project -->Build All which is same as: ctrl+B

Run: Right Click(Your PostfixEvaluator.java) -> Run As -> Java Application

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200296

Since this is not a compiler error, but a runtime error, it means that you changed the source code, but failed to recompile it, or at least failed to run the recompiled code.

The error definitely concerns an issue with a switch statement in the source code, but you have deleted that from your source code file. It must be therefore that you are loading a stale version of .class file. Check whether the Build Automatically option is activated and/or do a full project clean + rebuild.

Upvotes: 6

Related Questions