This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 4 years ago.
How do I make a method that receives from the user a Mathematical function represented by (x) like (x + 2), then receives a number which is (x) .. then return the result !! ..
Here's a link that gives a example on how to implement the shunting yard algorithm for expression parsing:
https://eddmann.com/posts/shunting-yard-implementation-in-java/
You can use the ideas in this to build your expression parser and read the stack of commands using switch statements to perform the operation entered. Good luck on the project!
If you mean what i think you do, you just need to make a function with a parameter, do the calculation and return the result.
Related
This question already has answers here:
Is there an eval() function in Java?
(14 answers)
Closed 5 years ago.
In java the following code works:
double a = 15.5+0.5;
System.out.println(a);
This will print 16.0.
So why does the following return a runtime error:
String a = "15.5+0.5";
Double b = Double.parseDouble(a);
System.out.println(b);
How can I get the second example to not give an error and behave like the first when converting the string to double and calculating a value?
parseDouble, parseInteger etc. don't perform expression evaluation, they just perform conversions from the string representation of some value to the numeric representation (so "5.0" gets converted to 5.0).
See the documentation here for the precise definition of parseDouble's behaviour.
If you want to perform expression evaluation, you could look into Reverse Polish Notation and the Shunting Yard algorithm, or ideally a library designed for this. Alternatively, you can use a more "hacky" solution.
This question already has answers here:
Fuzzy string search library in Java [closed]
(8 answers)
Closed 6 years ago.
I am working on a game where the user has to fill in a name of a celebrity. If the name is not 100% correct but nearly correct, the compare should succeed. Is there a ready to use function in java or something someone ever has written so I can use it ?
What you are looking for is the Levenshtein algortithm
You'll find here some Java implementations : https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java
Or, if you don't want/need to understand how it works, you can get the score directly from Apache StringUtils : getLevenshteinDistance
And if you want to get the percentage of similarities, you can do :
int lev = StringUtils.getLevenshteinDistance(s1, s2);
double ratio = ((double) lev) / (Math.max(s1.length, s2.length));
This question already has answers here:
Is there an eval() function in Java?
(14 answers)
Closed 6 years ago.
I need to write a java program which reads rules (conditions) from a file. The rules have to be transformed as conditions in Java. I don't know how to transform the rules specified as strings to Java source code.
For example, the rules like:
x != y
or
n >= 0 && s == n
should be transformed to Java source code as:
if(x != y){......}
else{....}
How can I made this possible?
May be my answer looks like a cannon against sparrow, but look at JRule the engine that allows you create flexible system of business rules inside java application
Another option is Jess. I've used it in the past with good success for handling rules-based activities.
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 8 years ago.
so I am looking for advice on how to solve a calculation that is entered as a String in Java.
For example: "50 * 6 - 75 / 25" is entered I am then needing to solve this calculation using bodmas to.
So (50*6)-(75/25)
Answer = 297
I didn't explain it very well but any help would be appreciated.
You can use the BeanShell's Interpreter class to do this.
new Interpreter().eval("50 * 6 - 75 / 25");
Or you can also use the java's inbuilt JavaScript engine, an example of which is shown below
Evaluating a math expression given in string form
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 9 years ago.
I want to parse an operator say "+" from the string "+" which i entered as command-line argument at run-time and then add two integers say 'a' and 'b'.
So how can i perform the above task?
What nobody so far is telling you is that to recognize arithmetic expressions in general you need to use, or write, a parser. Have a look for the Shunting-yard algorithm, recursive descent expression parsing, etc.
If you are using Java 1.7, you can use a switch to test for each possible operator and then do the corresponding operation:
switch(operator){
case("+"): result = a + b; break;
case("-"): result = a - b; break;
}
For older versions of Java can be done using if statements.
if (string.equals("+")) {
System.out.println("The result is " + (a + b));
}