如何在java / android中将字符串转换为布尔条件?

时间:2015-07-29 07:54:16

标签: java android regex string

我正在研究调查应用程序并从服务器获取数据。我在将String转换为布尔条件时遇到问题,布尔条件是String格式。 这是条件。我在网上搜索过很多但没找到解决办法。 我已经阅读了一些与之相关的帖子,但没有帮助。

http://www.java-examples.com/convert-java-string-object-boolean-object http://beginnersbook.com/2014/07/convert-string-to-boolean-primitive-in-java/ http://www.tutorialspoint.com/java/lang/boolean_parseboolean.htm

String s = "{hidSection=if(((Q4a.NAOK==\"5\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"7\")|| \n" +
            "            (Q4a.NAOK==\"7\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"7\" && Q4b.NAOK==\"7\")|| \n" +
            "            (Q4a.NAOK==\"8\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"7\"))\n" +
            "    ,1,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"4\" && Q4b.NAOK==\"7\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"5\")||\n" +
            "            (Q4a.NAOK==\"5\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"5\")|| \n" +
            "            (Q4a.NAOK==\"7\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"7\")|| \n" +
            "            (Q4a.NAOK==\"11\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"5\"))\n" +
            "    ,2,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"5\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"4\")|| \n" +
            "            (Q4a.NAOK==\"7\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"5\")|| \n" +
            "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"6\")|| \n" +
            "            (Q4a.NAOK==\"11\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"2\")|| \n" +
            "            (Q4a.NAOK==\"12\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"4\"))\n" +
            "    ,3,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"6\")|| \n" +
            "            (Q4a.NAOK==\"3\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"3\")|| \n" +
            "            (Q4a.NAOK==\"6\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"4\")|| \n" +
            "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"4\"))\n" +
            "    ,4,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"4\")||                (Q4a.NAOK==\"3\" && Q4b.NAOK==\"5\")|| \n" +
            "            (Q4a.NAOK==\"4\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"1\")|| \n" +
            "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"4\")|| \n" +
            "            (Q4a.NAOK==\"11\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"3\"))\n" +
            "    ,5,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"1\" && Q4b.NAOK==\"6\")|| \n" +
            "            (Q4a.NAOK==\"1\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"2\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"2\")|| \n" +
            "            (Q4a.NAOK==\"3\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"2\")|| \n" +
            "            (Q4a.NAOK==\"5\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"2\")|| \n" +
            "            (Q4a.NAOK==\"8\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"2\")||                \n" +
            "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"2\"))\n" +
            "    ,6,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"2\"))\n" +
            "    ,7,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"1\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"2\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"1\") || \n" +
            "            (Q4a.NAOK==\"3\" && Q4b.NAOK==\"1\"))\n" +
            "    ,8,\"\"))))))))}";

我跟着link。但是没有帮助我。如何将字符串转换为布尔条件?

1 个答案:

答案 0 :(得分:1)

从服务器返回代码非常可怕。无论如何,我给你的代码做了一些步骤:

  1. 从输入中将新行和多空格移除到一个长度空间中。
  2. 按照模式查找单词,其中单词以if开头,以数字结尾。将它们添加到数组中。
  3. 准备模式,用户选择的选项组合成类似于(Q4a.NAOK==\"7\" && Q4b.NAOK==\"5\")(答案1和答案2)。
  4. 检查数组是否包含第3点中的单词。如果找到,则保留数组中的单词并用逗号分隔。分裂的第二个词就是答案。
  5. private void doStuff() {
        int answer1 = 2;
        int answer2 = 1;
    
        String s = "{hidSection=if(((Q4a.NAOK==\"5\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"7\")|| \n" +
                "            (Q4a.NAOK==\"7\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"7\" && Q4b.NAOK==\"7\")|| \n" +
                "            (Q4a.NAOK==\"8\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"7\"))\n" +
                "    ,1,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"4\" && Q4b.NAOK==\"7\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"5\")||\n" +
                "            (Q4a.NAOK==\"5\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"5\")|| \n" +
                "            (Q4a.NAOK==\"7\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"7\")|| \n" +
                "            (Q4a.NAOK==\"11\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"5\"))\n" +
                "    ,2,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"5\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"4\")|| \n" +
                "            (Q4a.NAOK==\"7\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"5\")|| \n" +
                "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"6\")|| \n" +
                "            (Q4a.NAOK==\"11\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"2\")|| \n" +
                "            (Q4a.NAOK==\"12\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"4\"))\n" +
                "    ,3,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"6\")|| \n" +
                "            (Q4a.NAOK==\"3\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"3\")|| \n" +
                "            (Q4a.NAOK==\"6\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"4\")|| \n" +
                "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"4\"))\n" +
                "    ,4,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"4\")||                (Q4a.NAOK==\"3\" && Q4b.NAOK==\"5\")|| \n" +
                "            (Q4a.NAOK==\"4\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"1\")|| \n" +
                "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"4\")|| \n" +
                "            (Q4a.NAOK==\"11\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"3\"))\n" +
                "    ,5,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"1\" && Q4b.NAOK==\"6\")|| \n" +
                "            (Q4a.NAOK==\"1\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"2\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"2\")|| \n" +
                "            (Q4a.NAOK==\"3\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"2\")|| \n" +
                "            (Q4a.NAOK==\"5\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"2\")|| \n" +
                "            (Q4a.NAOK==\"8\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"2\")||                \n" +
                "            (Q4a.NAOK==\"9\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"2\"))\n" +
                "    ,6,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"2\"))\n" +
                "    ,7,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"1\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"2\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"1\") || \n" +
                "            (Q4a.NAOK==\"3\" && Q4b.NAOK==\"1\"))\n" +
                "    ,8,\"\"))))))))}";
    
        String answer = getAnswer(answer1, answer2, s);
        Log.d("xxx", "answer:" + answer);
    }
    
    private String getAnswer(int answer1, int answer2, String s) {
        //remove spaces 2 or more length and replace to 1
        s = s.replaceAll("\n", "");
        s = s.replaceAll("\\s+", " ");
    
        ArrayList<String> states = new ArrayList<String>();
    
        Pattern p = Pattern.compile("if\\({3}.*?\\,\\d+");
        Matcher m = p.matcher(s);
        while (m.find()) {
            states.add(m.group());
        }
    
        String stateFormat = "Q4a.NAOK==\"%d\" && Q4b.NAOK==\"%d\"";
        String stateFormatted = String.format(stateFormat, answer1, answer2);
    
        for (int i = 0; i < states.size(); i++) {
            if (states.get(i).contains(stateFormatted)) {
                String[] items = states.get(i).split(",");
                if (items.length > 1) {
                    return items[1].trim();
                }
            }
        }
    
        return null;//or null or integer -1
    }