1

I'm making a spreadsheet in Java.

I'd like to make a copy of the Microsoft Excel function ISLOGICAL. It checks whether a value is a logical value (TRUE or FALSE), and returns TRUE or FALSE.

All cells are arrays of string. This is what I have now:

public static String islogical(String[] value){
        String err = "#ERROR!";
        if (value.length != 1){
            return err;
        }

        boolean a = false;

        try{
            a = Boolean.parseBoolean(value[0]);
        }
        catch(IllegalArgumentException e){return err;}

        String ans = "FALSE";

        if(a){
            ans = "TRUE";
            return ans;
        }
        else{
            return ans;
        }

    }

I don't understand why, if my string value = "8>3", that it doesn't give me back true. All inputs, except the string = true, gives back false.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177

4 Answers4

1

I don't understand why, if my 'string value = "8>3"', that it doesn't give me back 'true'

That happens because parseBoolean() does not evaluate expressions.

From the Javadoc:

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Example: Boolean.parseBoolean("True") returns true.

Example: Boolean.parseBoolean("yes") returns false.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Boolean.parseBoolean() will return the boolean representation of a string. So "True" returns true, every thign else returns false.

What you want is to evaluate the string - check out https://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public static String islogical(String[] value){
    String err = "#ERROR!";
    if (value.length != 1){
        return err;
    }

    boolean a = false;

    try{
        a = engine.eval(value[0]);
    }
    catch(IllegalArgumentException e){return err;}

    String ans = "FALSE";

    if(a){
        ans = "TRUE";
        return ans;
    }
    else{
        return ans;
    }

}
ansible
  • 3,569
  • 2
  • 18
  • 29
0

Have you tried using the eval method? Here's an example from Is there an eval() function in Java?:

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");        
    Object result = engine.eval("3+4");
Community
  • 1
  • 1
user3204117
  • 141
  • 1
  • 1
  • 4
0

The parameters for function evaluation must have simple data types, like numbers, booleans, strings, etc. All subexpressions must be calculated before you make a call for your function.

The expression “=ISLOGICAL(8>3)” must be evaluated in the following order:

  1. Calculation of “8>3” subexpression. The result is a boolean value TRUE. The expression now looks like =ISLOGICAL(TRUE);
  2. Function ISLOGICAL evaluation on the TRUE argument. The result is TRUE.

If your expression is =ISLOGICAL(“8>3”) then your function correctly returns FALSE, because the "8>3” has string data type, not boolean. The best way for evaluation of excel-like expressions is the use of reverse-polish notation. Please follow wiki for details.