[英]Java String to complete mathematical sum
In java, I am currently in the process of trying to find a way to do mathematical sums by typing in text to a JTextField, putting it into a String, and converting it to an int and solving.在 java 中,我目前正在尝试找到一种方法来进行数学求和,方法是将文本输入 JTextField,将其放入 String,然后将其转换为 int 并求解。 The problem is however, I don't want to simply put a number into the string but an actual sum including addition, subtraction etc.然而,问题是,我不想简单地将数字放入字符串中,而是将实际总和包括加法、减法等。
Currently it will accept just doing something like, 1 which will go from string and convert to an int, but when I do '1+1' or even just '1+' it throws exceptions everywhere because '+' isn't a number.目前它会接受只做类似的事情,1 将从字符串转换为 int,但是当我做 '1+1' 甚至只是 '1+' 时,它会在任何地方抛出异常,因为 '+' 不是数字.
Which I already understand that it wont work because int's only allow numbers.我已经知道它不起作用,因为 int 只允许数字。
Is there a way I can safely type a complete sum into the text field and convert it to an int somehow?有没有办法可以安全地在文本字段中输入完整的总和并将其转换为 int ? Like typing 7-2*5 and then the answer being held in an int?就像输入 7-2*5 然后将答案保存在 int 中一样?
Thanks.谢谢。
You can use the JavaScript
engine:您可以使用JavaScript
引擎:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String expression = textField.getText();
System.out.println(engine.eval(expression));
Edit to allow all equations:编辑以允许所有方程:
All you have to do now to allow things like sin, cos, tan is this:您现在要做的就是允许诸如sin、cos、tan 之类的事情:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String expression = textField.getText();
// Between here...
expression = expression.
replace("sin", "Math.sin").
replace("cos", "Math.cos").
replace("tan", "Math.tan").
replace("sqrt", "Math.sqrt").
replace("log", "Math.log").
replace("pi", "Math.PI");
// And so on...
System.out.println(engine.eval(foo));
So then you can do something like:那么你可以做这样的事情:
5 + 5 - 2 / 5 + sin(55) - log(20)
Anything you want.任何你想要的。
This will do it for you.这将为您做到。
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class Test {
public static void main(String[] args) throws Exception{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";//any mathematical operations +,-,*,/
System.out.println(engine.eval(foo));
}
}
Here you can put the methods describing the log/cos etc functions您可以在这里放置描述 log/cos 等功能的方法
String st = "10+3";
int result;
for(int i=0;i<st.length();i++)
{
if(st.charAt(i)=='+')
{
result=Integer.parseInt(st.substring(0, i))+Integer.parseInt(st.substring(i+1, st.length()));
System.out.print(result);
}
}
See to java.util.regex.Matcher or java.util.regex.Pattern For ex:请参阅 java.util.regex.Matcher 或 java.util.regex.Pattern 例如:
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class IpDnsValidateUtils {
public static final int MAX_DNS_LEN = 39;
private static final Pattern ptrnIp = Pattern
.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
private static final Pattern ptrnDomainName = Pattern
.compile("^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])){0,}[.][a-zA-Z]{2,5}$");
public static boolean isValidIp(String value, boolean emptyTrue) { boolean ret = false; if (!value.isEmpty() || !emptyTrue) {
Matcher m = ptrnIp.matcher(value);
ret = m.matches(); } else {
ret = true; } return ret;
}
public static boolean isValidDns(String value, boolean emptyTrue) { boolean ret = false; if (!value.isEmpty() || !emptyTrue) {
Matcher m = ptrnDomainName.matcher(value);
ret = m.matches(); } else {
ret = true; } return ret;
} }
or StringTokenizer class或 StringTokenizer 类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.