Solving a calculation with a java string [duplicate] - java

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

Related

Java Mathematical Functions By User [duplicate]

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.

How to round off prices using Java [duplicate]

This question already has answers here:
Best method to round up to the nearest 0.05 in java
(4 answers)
Closed 4 years ago.
for example the program should be able to execute this task.
21.34 should be displayed as 21.35,
21.32 should be displayed as 21.30,
21.36 should be displayed as 21.35,
21.38 should be displayed as 21.40
please explain or give me code samples on how to make this work. or is it even possible? Thanks!
If you want to round a double to the nearest multiple of 0.05 you can do
d = Math.round(d * 20) / 20.0;
In countries like Australia you need to round to a multiple of 5c.
please explain
As you are asking the question, you really need to be able to explain it.

How do I generate an exponentially distributed number in Java (given a mean)? [duplicate]

This question already has answers here:
Pseudorandom Number Generator - Exponential Distribution
(9 answers)
Java exponential distribution
(1 answer)
Closed 5 years ago.
I am given a mean of 5. I need to generate a random number (exponentially distributed) in Java.
I know for Python, you can just run something like random.expovariate(5), but I'm not sure how to solve this for Java. Can anyone help me out?
see here. What you are looking for is something like this:
public double getNext() {
return Math.log(1-rand.nextDouble())/(-lambda);
}
(code taken from here)

How to compare two strings in Java with error margin [duplicate]

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));

Exponent Math in Java [duplicate]

This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
Could anyone explain to me why
System.out.println(100*(1-10^(-10/10)));
results in the number "800" being printed out? The correct answer is 90 when you use a calculator. How would I go about doing this calculation in Java?
Thanks!
The ^ operator does not do what you think it does. It is bitwise-xor
You need to look into the Math.pow() method.

Categories

Resources