首页 > Java > java教程 > 正文

Java数据处理的实例代码

零下一度
发布: 2017-06-25 10:19:05
原创
1635人浏览过

  对于形如“(type=sita##)&&(((ctyp=fpl##)||(ctyp=chg##)||(ctyp=cnl##)||(ctyp=dla##)||(ctyp=dl##)||(ctyp=ed##))&&(adep=zsss##))”的字符串,常以某个事物的判定条件出现,要计算出整个条件最后的结果(true/false)。

  以下给出Java代码

  1 package com.test;  2   3 import net.sourceforge.jeval.Evaluator;  4   5 public class T1 {  6   7     public static void main(String[] args) throws Exception {  8         String condition ="(TYPE=SITA##)&&(((CTYP=FPL##)||(CTYP=CHG##)||(CTYP=CNL##)||(CTYP=DLA##)||(CTYP=DL##)||(CTYP=ED##))&&(ADEP=ZSSS##))";  9         condition = replaceExpression(condition, "TYPE=", "SITA", "="); 10         condition = replaceExpression(condition, "CTYP=", "FPL", "="); 11         condition = replaceExpression(condition, "ADEP=", "", "="); 12         System.out.println("condition:"+condition);  //condition:(1)&&(((1)||(0)||(0)||(0)||(0)||(0))&&(0)) 13          14         /** 15          * 数字表达式解析工具 16          *    Evaluator需要下载jeval包,本文后附上链接 17          */ 18         Evaluator evaluator = new Evaluator(); 19         boolean b = evaluator.getBooleanResult(condition);  // false 20         System.out.println(b);        
 21         System.out.println(evaluator.evaluate("2+3"));  // 5.0 22          23     } 24      25     /** 26      * 
 27      * @param condition 28      * @param replace 29      * @param toBeCompared 30      * @param operators 31      * @return 32      * @throws Exception 对于不符合计算的会抛出异常 33      */ 34     public static String replaceExpression(String condition, String replace, 35             String toBeCompared, String operators) throws Exception { 36         while (condition.contains(replace)) { 37             int start = condition.indexOf(replace); 38             int end = condition.indexOf("##", start); 39             String express = condition.substring(start, end); 40             express = express.substring(replace.length()); 41             if (">=".equals(operators)) { 42  43                 if (Long.parseLong(toBeCompared) >= Long.parseLong(express)) { 44                     condition = condition.substring(0, start) + "1" 45                             + condition.substring(end + 2); 46                 } else { 47                     condition = condition.substring(0, start) + "0" 48                             + condition.substring(end + 2); 49                 } 50             } else if ("<=".equals(operators)) { 51                 if (Long.parseLong(toBeCompared) <= Long.parseLong(express)) { 52                     condition = condition.substring(0, start) + "1" 53                             + condition.substring(end + 2); 54                 } else { 55                     condition = condition.substring(0, start) + "0" 56                             + condition.substring(end + 2); 57                 } 58             } else if (">".equals(operators)) { 59  60                 if (Long.parseLong(toBeCompared) > Long.parseLong(express)) { 61                     condition = condition.substring(0, start) + "1" 62                             + condition.substring(end + 2); 63                 } else { 64                     condition = condition.substring(0, start) + "0" 65                             + condition.substring(end + 2); 66                 } 67             } else if ("<".equals(operators)) { 68                 if (Long.parseLong(toBeCompared) < Long.parseLong(express)) { 69                     condition = condition.substring(0, start) + "1" 70                             + condition.substring(end + 2); 71                 } else { 72                     condition = condition.substring(0, start) + "0" 73                             + condition.substring(end + 2); 74                 } 75             } else if ("!=".equals(operators)) { 76                 if (!express.equals(toBeCompared)) { 77                     condition = condition.substring(0, start) + "1" 78                             + condition.substring(end + 2); 79                 } else { 80                     condition = condition.substring(0, start) + "0" 81                             + condition.substring(end + 2); 82                 } 83             } else { 84                 if ("RADDRESS=".equals(replace)) { 85                     String[] toBeComparedArray = toBeCompared.split(" "); 86                     String flag = "0"; 87                     for (String toBeComparedString : toBeComparedArray) { 88                         if (express.equals(toBeComparedString)) { 89                             flag = "1"; 90                             break; 91                         } 92                     } 93                     condition = condition.substring(0, start) + flag 94                             + condition.substring(end + 2); 95  96                 } else { 97                     if (express.equals(toBeCompared)) { 98                         condition = condition.substring(0, start) + "1" 99                                 + condition.substring(end + 2);100                     } else {101                         condition = condition.substring(0, start) + "0"102                                 + condition.substring(end + 2);103                     }104                 }105             }106         }107         return condition;108     }109 }
登录后复制

 相关jar包下载:

代码小浣熊
代码小浣熊

代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

代码小浣熊 51
查看详情 代码小浣熊

以上就是Java数据处理的实例代码的详细内容,更多请关注php中文网其它相关文章!

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号