String str1="1<=2";通过java来判断str是否为true或者false?
解决方案 »
- 这个怎么实现!!!!1!
- GC.fillPolygon颜色不准确
- 帮忙看一下几行代码,赋值出错!
- java7要发布了,大家来说说性特性吧
- 使用jacob时总是报错“com.jacob.com.ComFailException: Can't get object clsid from progid”这是怎么回事?
- 项目紧急求助,关于java编写的飞机救援问题
- java报错,并在桌面生成一个hs_err_pid3900.log文件,请各位帮忙分析一下,谢谢!
- 绝对新手问题:::怎样设置classpath的路径?????
- Socket的问题?
- 有谁知道<br data-filtered="filtered">是什么
- 关于子类调用父类private函数的疑惑。
- 求教高手
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
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();
}
}
return str;
}然后通过反射调用该方法,就OK了。
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));
}
}看看这样的可以不?刚写的
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();
}
}
}