String str1="1<=2";通过java来判断str是否为true或者false?

解决方案 »

  1.   

    这个需要自己去写吧......参考链接http://topic.csdn.net/t/20060630/11/4852525.html
      

  2.   

    import java.util.*;
    public class Test 
    {
    public static void main(String[] args) 
    {
    String str1 = "1<=2";
    int[] num = new int[2];
    StringTokenizer str = new StringTokenizer(str1,"<=");
    int i=0;
    while (str.hasMoreTokens())
    {
    num[i] = Integer.parseInt(str.nextToken());
    i++;
    }
    System.out.println(num[0]<=num[1]);
    }
    }测试结果true
      

  3.   

    建议你看看apache的bsf项目http://jakarta.apache.org/bsf/index.html支持各种方式的脚本
      

  4.   

    public static boolean tryBoolean(String aStatement) throws IOException{
      String sProjectPath = "c:\\projectName\\"; //工程所在目录  FileWriter fw = new FileWriter(sProjectPath + "test\\TryBoolean.java");
      try{
        fw.write("package test;\n");
        fw.write("public class TryBoolean{\n");
        fw.write("  public boolean tryBoolean(String aStatement){\n");
        fw.write("    return " + aStatement + ";\n");
        fw.write("  }\n");
        fw.write("}\n");
      }finally{
        fw.close();
      }
      Main.compile(new String[] { "-cp", sProjectPath, "-d", sProjectPath,
            sProjectPath + "test\\TryBoolean.java" }, new PrintWriter(
            System.out));
      MyClassLoader mc = new MyClassLoader(aClassPath);
      Class c = mc.findClass(sFullClassName);
      Object o = c.newInstance();
      Method m = c.getMethod("dance", null);
      Object r = m.invoke(o, new Object[] {});
      return Boolean.valueOf(r.toString());
    }
    /////////////////////////////////////
    public class MyClassLoader extends ClassLoader {  
     private String basepath; public MyClassLoader(String basepath) {
      this.basepath = basepath;
     } public MyClassLoader(ClassLoader parent, String basepath) {
      super(parent);
      this.basepath = basepath;
     } public Class findClass(String className) throws ClassNotFoundException {
      byte classData[];
      classData = getTypeFromBasePath(className);
      if (classData == null) {  }
      return defineClass(className, classData, 0, classData.length);
     } public byte[] getTypeFromBasePath(String typeName) {
      FileInputStream fis = null;
      String fileName = basepath + File.separatorChar + typeName.replace('.', File.separatorChar)
        + ".class";
      try {
       fis = new FileInputStream(fileName);
      } catch (Exception e) {
       e.printStackTrace();
       return null;
      }
      BufferedInputStream bis = new BufferedInputStream(fis);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      try {
       int c = bis.read();
       while (c != -1) {
        out.write(c);
        c = bis.read();
       }
      } catch (Exception e) {
       e.printStackTrace();
       return null;
      }
      return out.toByteArray();
     }
    }
      

  5.   

    字节码增强吧,写一个空的类,运行期在类里面注入方法 public static boolean getResult(){
    return str;
    }然后通过反射调用该方法,就OK了。
      

  6.   

    http://ruyuntao.javaeye.com/blog/406935  我到google去搜索的答案,楼主你要给我分呀
      

  7.   


    package csdn;import java.util.ArrayList;
    import java.util.List;public class StringOperation {

    private double firstValue = 0.0d;
    private double secondValue = 0.0d;
    private List<String> operations = new ArrayList<String>();
    private int index = 0;
    private int pos = 0;
    private String resolve = null;

    /*
     * 支持<=,>=,<>(!=).
     * S=N+O+N
     * N=L+P+L
     * P="."|""
     * L=0|1|2|3|4|5|6|7|8|9|L+L
     * O="<="|">="|"<>"
     */
    public boolean decode(final String str) throws Exception{
    resolve = str.replaceAll(" ", "");
    while((resolve.charAt(pos)>='0' && resolve.charAt(pos)<='9')
    || resolve.charAt(pos) == '.')
    pos++;
    firstValue = Double.parseDouble(resolve.substring(index,pos));
    index = pos;
    if(isOperation(String.valueOf(resolve.charAt(pos)))){
    operationsResolve();
    }else{
    throw new Exception("Format Error!");
    }
    secondValue = Double.parseDouble(resolve.substring(index));
    index = pos;
    return getResult();
    }

    private boolean getResult(){
    boolean flag = false;
    // System.out.println("FirstValue:"+firstValue);
    // System.out.println("SecondValue:"+secondValue);
    for(String str:operations){
    if("<".equals(str)){
    flag = flag || (firstValue < secondValue);
    }else if(">".equals(str)){
    flag = flag || (firstValue > secondValue);
    }else{
    flag = flag || (firstValue == secondValue);
    }
    }
    clear();
    return flag;
    }
    private void clear(){
    operations.clear();
    firstValue = 0.0d;
    secondValue = 0.0d;
    resolve = null;
    index = 0;
    pos = 0;
    }
    private boolean isOperation(String str){
    if("<".equals(str))
    return true;
    if("=".equals(str))
    return true;
    if(">".equals(str))
    return true;
    return false;
    }
    private void operationsResolve() throws Exception{
    while(true){
    String operation = String.valueOf(resolve.charAt(pos));
    if(isOperation(operation)){
    operations.add(operation);
    pos++;
    if(pos >= resolve.length()-1){
    throw new Exception("Format Error!");
    }
    }else{
    index = pos;
    return;
    }
    }
    }

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
    StringOperation so = new StringOperation();
    String resolve = "112.3<=223.5";
    System.out.println(resolve+":"+so.decode(resolve));
    resolve = "22 > 33";
    System.out.println(resolve+":"+so.decode(resolve));
    resolve = "33<>44";
    System.out.println(resolve+":"+so.decode(resolve));
    resolve = "44<>44";
    System.out.println(resolve+":"+so.decode(resolve));
    }
    }看看这样的可以不?刚写的
      

  8.   

    典型的脚本应用,如果是jdk1.6用下面的方法,jdk1.5或以下自己去下载mozzila的rhino
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;public class JavaAndScript {
    public static void main(String[] args){
    ScriptEngineManager factory=new ScriptEngineManager();
    ScriptEngine engine=factory.getEngineByName("JavaScript");

    try{
    String str1="1 <=2";
    System.out.println(engine.eval(str1)); }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    }