java - 字符串作为 boolean 值

标签 java string boolean spreadsheet

我正在用 Java 制作电子表格。

我想复制 Microsoft Excel 函数 ISLOGICAL。 它检查一个值是否为逻辑值(TRUE 或 FALSE),并返回 TRUE 或 FALSE。

所有单元格都是字符串数组。这就是我现在拥有的:

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;
        }

    }

我不明白为什么,如果我的字符串值=“8>3”,它不会返回true。除字符串 = true 之外的所有输入都会返回 false

最佳答案

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

发生这种情况是因为 parseBoolean() 不计算表达式。

来自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.

关于java - 字符串作为 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21172880/

相关文章:

c# - 使用相同 boolean 值的两种形式

java - Java 中 goto 语句的替代方法

java - 使用Java创建视频播放器

java - 如何以 MB(兆字节)为单位获取文件的大小?

java - 验收测试增量值

c++ - 如何将 std::string 转换为 const char* 或 char*

c - 字符串比较在 C 中不起作用

c# - 更改 boolean 值的默认值

c - 该函数如何工作!(n & 1),为什么! before () ,为什么是 n & 1 ,是什么意思?

java - 如何限制用户只能在 java JTextField 中输入一个空格键