java中有没有像js中eval函数的方法? 有一个字符串“4+3”我在java中怎么能执行4+3得到7呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在JAVA中用反射可以实现类似的功能 在JAVA中我还没有见过类似Javascript中eval的这种功能除非自己写一个组件来实现,我说的用反射进行就是下面的代码.import java.lang.reflect.*;public class ReflactTest{ public static void main(String[] args){ try{ Class addClass=Class.forName("AddTest"); Method[] method=addClass.getMethods(); System.out.println(method[0].invoke(new AddTest(),new Object[]{3,4})); } catch (Exception e){ e.getMessage(); } } }class AddTest{ private int add1; private int add2; public AddTest(){} public int add(int add1,int add2){ return add1+add2; }} 因为JAVA是一个面向对象的语言,我想象JS中的那种功能放到JAVA中不太适合.在JAVA中,语句都是在某一个类之中的,没有独立的语句存在.因此我想这种功能不会在JAVA API中出现,当然,这只是我的想法,未来的事情谁都说不清楚... 早已有高人写过这个了,google一下就有了 代码太长,分两部分贴上package mathTools;/** * Eval.java * * Created on 2006年4月10日, 下午3:46 * * To change this template, choose Tools | Template Manager * and open the template in the editor. * * 支持运算符:+ - * / % > < ] [ ! | & = * 其中: * ] 表示大于等于 * [ 表示小于等于 * ! 表示不等于 * | 表示或 * & 表示与 * = 表示是否等于 * * 支持函数:sqrt,square,ceil,sin,cos,asin,acon.tan.atan,log,exp 具体含义见 calFunction 代码 * *//** * * @author Trumplet * */import java.util.Stack;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Eval{ public static String OPTS="+-*/%><][!|&=#"; public Object calculate(String expression) throws ExpressionException{ try { Stack Opts=new Stack(); Stack Values=new Stack(); String exp=expression+"#"; int nCount=exp.length(),nIn,nOut,nTemp; Opts.push("#"); String temp="",optOut="",optIn="",value1="",value2="",optTemp="",opt="",temp1=""; int nFun=0; boolean isFun=false; for(int i=0;i<nCount;){ nTemp=0; opt=exp.substring(i,i+1); isFun=false; temp1=""; while(i<nCount){ if(!temp1.equals("")){ if(opt.equals("(")){ nFun++; isFun=true; } else if(opt.equals(")")){ nFun--; } } if((nFun>0)||((!isFun)&&this.isValue(opt))){ temp1+=opt; nTemp++; opt=exp.substring(i+nTemp,i+nTemp+1); } else{ if(isFun){ temp1+=opt; nTemp++; } break; } } if(temp1.equals("")){ temp=opt; } else{ temp=temp1; } if(nTemp>0){ i=i+nTemp-1; } temp=temp.trim(); if(this.isValue(temp)){ temp=this.getValue(temp); Values.push(temp); i++; } else{ optIn=Opts.pop().toString(); nIn=this.getOptPriorityIn(optIn); nOut=this.getOptPriorityOut(temp); if(nIn==nOut){ i++; } else if(nIn>nOut){ String ret=""; value1=Values.pop().toString(); value2=Values.pop().toString(); ret=String.valueOf(this.calValue(value2,optIn,value1)); Values.push(ret); } else if(nIn<nOut){ Opts.push(optIn); Opts.push(temp); i++; } } } return Values.pop(); } catch(ExpressionException eE) { throw eE; } catch(Exception e) { throw new ExpressionException("表达式"+expression+"格式非法!"); } } protected int getOptPriorityOut(String opt)throws ExpressionException{ if(opt.equals("+")){ return 1; } else if(opt.equals("-")){ return 2; } else if(opt.equals("*")){ return 5; } else if(opt.equals("/")){ return 6; } else if(opt.equals("%")){ return 7; } else if(opt.equals(">")){ return 11; } else if(opt.equals("<")){ return 12; } else if(opt.equals("]")){ return 13; } else if(opt.equals("[")){ return 14; } else if(opt.equals("!")){ return 15; } else if(opt.equals("|")){ return 16; } else if(opt.equals("&")) { return 23; } else if(opt.equals("=")) { return 25; } else if(opt.equals("#")) { return 0; } else if(opt.equals("(")){ return 1000; } else if(opt.equals(")")){ return -1000; } throw new ExpressionException("运算符"+opt+"非法!"); } protected int getOptPriorityIn(String opt) throws ExpressionException{ if(opt.equals("+")){ return 3; } else if(opt.equals("-")){ return 4; } else if(opt.equals("*")){ return 8; } else if(opt.equals("/")){ return 9; } else if(opt.equals("%")){ return 10; } else if(opt.equals(">")){ return 17; } else if(opt.equals("<")){ return 18; } else if(opt.equals("]")){ return 19; } else if(opt.equals("[")){ return 20; } else if(opt.equals("!")){ return 21; } else if(opt.equals("|")){ return 22; } else if(opt.equals("&")) { return 24; } else if(opt.equals("=")) { return 26; } else if(opt.equals("(")){ return -1000; } else if(opt.equals(")")){ return 1000; } else if(opt.equals("#")){ return 0; } throw new ExpressionException("运算符"+opt+"非法!"); } protected String getOPTS() { return OPTS; } protected boolean isValue(String cValue){ String notValue=this.getOPTS()+"()"; return notValue.indexOf(cValue)==-1; } protected boolean isOpt(String value){ return this.getOPTS().indexOf(value)>=0; } protected double calValue(String value1,String opt,String value2) throws ExpressionException{ try { double dbValue1=Double.valueOf(value1).doubleValue(); double dbValue2=Double.valueOf(value2).doubleValue(); long lg=0; if(opt.equals("+")){ return dbValue1+dbValue2; } else if(opt.equals("-")){ return dbValue1-dbValue2; } else if(opt.equals("*")){ return dbValue1*dbValue2; } else if(opt.equals("/")){ return dbValue1/dbValue2; } else if(opt.equals("%")){ lg=(long)(dbValue1/dbValue2); return dbValue1-lg*dbValue2; } else if(opt.equals(">")){ if(dbValue1>dbValue2) return 1; else return 0; } else if(opt.equals("<")){ if(dbValue1<dbValue2) return 1; else return 0; } else if(opt.equals("]")){ if(dbValue1>=dbValue2) return 1; else return 0; } else if(opt.equals("[")){ if(dbValue1<=dbValue2) return 1; else return 0; } else if(opt.equals("!")){ if(dbValue1!=dbValue2) return 1; else return 0; } else if(opt.equals("|")){ if(dbValue1>0||dbValue2>0) return 1; else return 0; } else if(opt.equals("&")){ if(dbValue1>0&&dbValue2>0) return 1; else return 0; } else if(opt.equals("=")){ if(dbValue1==dbValue2) return 1; else return 0; } }catch(Exception e) { throw new ExpressionException("值"+value1+"或"+value2+"在进行"+ opt+"运算时非法!"); } throw new ExpressionException("运算符"+opt+"非法!"); } protected String getValue(String oldValue) throws ExpressionException{ String reg="^([a-zA-Z0-9_]+)\\(([a-zA-Z0-9_.()]+)\\)$"; if(this.isFunctionCal(oldValue)){ Pattern p=Pattern.compile(reg); Matcher m=p.matcher(oldValue); m.find(); return calFunction(m.group(1),m.group(2)); } return oldValue; } protected boolean isFunctionCal(String value){ String reg="^([a-zA-Z0-9_]+)\\(([a-zA-Z0-9_.()]+)\\)$"; return value.matches(reg); } protected String calFunction(String function,String value) throws ExpressionException{ String lowerFun=function.toLowerCase(); double db=0; try { db=Double.valueOf(this.getValue(value)).doubleValue(); if(lowerFun.equals("log")){ return String.valueOf(Math.log(db)); } else if(lowerFun.equals("square")){ return String.valueOf(Math.pow(db,2)); } else if(lowerFun.equals("sqrt")){ return String.valueOf(Math.sqrt(db)); } else if(lowerFun.equals("sin")){ return String.valueOf(Math.sin(db)); } else if(lowerFun.equals("asin")){ return String.valueOf(Math.asin(db)); } else if(lowerFun.equals("cos")){ return String.valueOf(Math.cos(db)); } else if(lowerFun.equals("tan")){ return String.valueOf(Math.tan(db)); } else if(lowerFun.equals("atan")){ return String.valueOf(Math.atan(db)); } else if(lowerFun.equals("ceil")){ return String.valueOf(Math.ceil(db)); } else if(lowerFun.equals("exp")){ return String.valueOf(Math.exp(db)); } }catch(Exception e) { throw new ExpressionException("函数"+function+"值"+value+"非法!"); } throw new ExpressionException("函数"+function+"不支持!"); } public static void main(String[]args) { Eval be=new Eval(); //String exp="sin(ceil(100))*29+20+30*3+0|0|1+1&1*5+2=2"; //String exp="sin(ceil(sqrt(100)))*29+20+30*3+0|0|1+1&1*5+2=2"; String exp="8/(3-8/3)"; try { System.out.println(be.calculate(exp)); } catch(ExpressionException eE) { System.out.println(eE.getMessage()); } } //表达式异常类代码: public class ExpressionException extends Exception{ public ExpressionException(String msg) { super(msg); } }} 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b 要不重复的随机数 java中 如何判定文本框(JTextFeld)的内容为空? lzo 听说这个压缩算法很不错? 谁来帮忙转为java?谢谢! 如何设置环境变量来定义初始的Java内存的大小 怎么样在A类中访问B类中的成员?急? 请问j2se,j2ee,j2me到底有什么区别啊? java适合做桌面型应用吗? 高手,在JBuilder环境中学JAVA 如何,请高手指点一下。我是一个新手。 高分求购做饼图、线形图的源吗? 怎样判断字符串"2006-16-25"不是正确的Date? 哈哈,对象的内存地址.好玩
import java.lang.reflect.*;public class ReflactTest{
public static void main(String[] args){
try{
Class addClass=Class.forName("AddTest");
Method[] method=addClass.getMethods();
System.out.println(method[0].invoke(new AddTest(),new Object[]{3,4}));
} catch (Exception e){
e.getMessage();
}
}
}class AddTest{
private int add1;
private int add2;
public AddTest(){}
public int add(int add1,int add2){
return add1+add2;
}
}
/**
* Eval.java
*
* Created on 2006年4月10日, 下午3:46
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* 支持运算符:+ - * / % > < ] [ ! | & =
* 其中:
* ] 表示大于等于
* [ 表示小于等于
* ! 表示不等于
* | 表示或
* & 表示与
* = 表示是否等于
*
* 支持函数:sqrt,square,ceil,sin,cos,asin,acon.tan.atan,log,exp 具体含义见 calFunction 代码
*
*//**
*
* @author Trumplet
*
*/import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Eval{
public static String OPTS="+-*/%><][!|&=#";
public Object calculate(String expression) throws ExpressionException{
try {
Stack Opts=new Stack();
Stack Values=new Stack();
String exp=expression+"#";
int nCount=exp.length(),nIn,nOut,nTemp;
Opts.push("#");
String temp="",optOut="",optIn="",value1="",value2="",optTemp="",opt="",temp1="";
int nFun=0;
boolean isFun=false;
for(int i=0;i<nCount;){
nTemp=0;
opt=exp.substring(i,i+1);
isFun=false;
temp1="";
while(i<nCount){
if(!temp1.equals("")){
if(opt.equals("(")){
nFun++;
isFun=true;
} else if(opt.equals(")")){
nFun--;
}
}
if((nFun>0)||((!isFun)&&this.isValue(opt))){
temp1+=opt;
nTemp++;
opt=exp.substring(i+nTemp,i+nTemp+1);
} else{
if(isFun){
temp1+=opt;
nTemp++;
}
break;
}
}
if(temp1.equals("")){
temp=opt;
} else{
temp=temp1;
}
if(nTemp>0){
i=i+nTemp-1;
}
temp=temp.trim();
if(this.isValue(temp)){
temp=this.getValue(temp);
Values.push(temp);
i++;
} else{
optIn=Opts.pop().toString();
nIn=this.getOptPriorityIn(optIn);
nOut=this.getOptPriorityOut(temp);
if(nIn==nOut){
i++;
} else if(nIn>nOut){
String ret="";
value1=Values.pop().toString();
value2=Values.pop().toString();
ret=String.valueOf(this.calValue(value2,optIn,value1));
Values.push(ret);
} else if(nIn<nOut){
Opts.push(optIn);
Opts.push(temp);
i++;
}
}
}
return Values.pop();
} catch(ExpressionException eE) {
throw eE;
} catch(Exception e) {
throw new ExpressionException("表达式"+expression+"格式非法!");
}
}
protected int getOptPriorityOut(String opt)throws ExpressionException{
if(opt.equals("+")){
return 1;
} else if(opt.equals("-")){
return 2;
} else if(opt.equals("*")){
return 5;
} else if(opt.equals("/")){
return 6;
} else if(opt.equals("%")){
return 7;
} else if(opt.equals(">")){
return 11;
} else if(opt.equals("<")){
return 12;
} else if(opt.equals("]")){
return 13;
} else if(opt.equals("[")){
return 14;
} else if(opt.equals("!")){
return 15;
} else if(opt.equals("|")){
return 16;
} else if(opt.equals("&")) {
return 23;
} else if(opt.equals("=")) {
return 25;
} else if(opt.equals("#")) {
return 0;
} else if(opt.equals("(")){
return 1000;
} else if(opt.equals(")")){
return -1000;
}
throw new ExpressionException("运算符"+opt+"非法!");
}
protected int getOptPriorityIn(String opt) throws ExpressionException{
if(opt.equals("+")){
return 3;
} else if(opt.equals("-")){
return 4;
} else if(opt.equals("*")){
return 8;
} else if(opt.equals("/")){
return 9;
} else if(opt.equals("%")){
return 10;
} else if(opt.equals(">")){
return 17;
} else if(opt.equals("<")){
return 18;
} else if(opt.equals("]")){
return 19;
} else if(opt.equals("[")){
return 20;
} else if(opt.equals("!")){
return 21;
} else if(opt.equals("|")){
return 22;
} else if(opt.equals("&")) {
return 24;
} else if(opt.equals("=")) {
return 26;
} else if(opt.equals("(")){
return -1000;
} else if(opt.equals(")")){
return 1000;
} else if(opt.equals("#")){
return 0;
}
throw new ExpressionException("运算符"+opt+"非法!");
}
protected String getOPTS() {
return OPTS;
}
protected boolean isValue(String cValue){
String notValue=this.getOPTS()+"()";
return notValue.indexOf(cValue)==-1;
}
protected boolean isOpt(String value){
return this.getOPTS().indexOf(value)>=0;
}
protected double calValue(String value1,String opt,String value2) throws ExpressionException{
try {
double dbValue1=Double.valueOf(value1).doubleValue();
double dbValue2=Double.valueOf(value2).doubleValue();
long lg=0;
if(opt.equals("+")){
return dbValue1+dbValue2;
} else if(opt.equals("-")){
return dbValue1-dbValue2;
} else if(opt.equals("*")){
return dbValue1*dbValue2;
} else if(opt.equals("/")){
return dbValue1/dbValue2;
} else if(opt.equals("%")){
lg=(long)(dbValue1/dbValue2);
return dbValue1-lg*dbValue2;
} else if(opt.equals(">")){
if(dbValue1>dbValue2)
return 1;
else
return 0;
} else if(opt.equals("<")){
if(dbValue1<dbValue2)
return 1;
else
return 0;
} else if(opt.equals("]")){
if(dbValue1>=dbValue2)
return 1;
else
return 0;
} else if(opt.equals("[")){
if(dbValue1<=dbValue2)
return 1;
else
return 0;
} else if(opt.equals("!")){
if(dbValue1!=dbValue2)
return 1;
else
return 0;
} else if(opt.equals("|")){
if(dbValue1>0||dbValue2>0)
return 1;
else
return 0;
} else if(opt.equals("&")){
if(dbValue1>0&&dbValue2>0)
return 1;
else
return 0;
} else if(opt.equals("=")){
if(dbValue1==dbValue2)
return 1;
else
return 0;
}
}catch(Exception e) {
throw new ExpressionException("值"+value1+"或"+value2+"在进行"+ opt+"运算时非法!");
}
throw new ExpressionException("运算符"+opt+"非法!");
}
protected String getValue(String oldValue) throws ExpressionException{
String reg="^([a-zA-Z0-9_]+)\\(([a-zA-Z0-9_.()]+)\\)$";
if(this.isFunctionCal(oldValue)){
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(oldValue);
m.find();
return calFunction(m.group(1),m.group(2));
}
return oldValue;
}
protected boolean isFunctionCal(String value){
String reg="^([a-zA-Z0-9_]+)\\(([a-zA-Z0-9_.()]+)\\)$";
return value.matches(reg);
}
protected String calFunction(String function,String value) throws ExpressionException{
String lowerFun=function.toLowerCase();
double db=0;
try {
db=Double.valueOf(this.getValue(value)).doubleValue();
if(lowerFun.equals("log")){
return String.valueOf(Math.log(db));
} else if(lowerFun.equals("square")){
return String.valueOf(Math.pow(db,2));
} else if(lowerFun.equals("sqrt")){
return String.valueOf(Math.sqrt(db));
} else if(lowerFun.equals("sin")){
return String.valueOf(Math.sin(db));
} else if(lowerFun.equals("asin")){
return String.valueOf(Math.asin(db));
} else if(lowerFun.equals("cos")){
return String.valueOf(Math.cos(db));
} else if(lowerFun.equals("tan")){
return String.valueOf(Math.tan(db));
} else if(lowerFun.equals("atan")){
return String.valueOf(Math.atan(db));
} else if(lowerFun.equals("ceil")){
return String.valueOf(Math.ceil(db));
} else if(lowerFun.equals("exp")){
return String.valueOf(Math.exp(db));
}
}catch(Exception e) {
throw new ExpressionException("函数"+function+"值"+value+"非法!");
}
throw new ExpressionException("函数"+function+"不支持!");
}
public static void main(String[]args) {
Eval be=new Eval();
//String exp="sin(ceil(100))*29+20+30*3+0|0|1+1&1*5+2=2";
//String exp="sin(ceil(sqrt(100)))*29+20+30*3+0|0|1+1&1*5+2=2";
String exp="8/(3-8/3)";
try {
System.out.println(be.calculate(exp));
} catch(ExpressionException eE) {
System.out.println(eE.getMessage());
}
}
//表达式异常类代码:
public class ExpressionException extends Exception{
public ExpressionException(String msg) {
super(msg);
}
}
}