#Java math equation converter

1 messages · Page 1 of 1 (latest)

dull elbow
#

I want to use an engine to convert a string of a simple math equation to an actual equation. The code compiles but the engine isn't found. Code: ```java
// Create a new Script engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");

    // Calculation
    try {
        Object result = engine.eval(equation);
        System.out.println("Result: " + result);
    } catch (ScriptException e) {
        System.out.println("ERROR: Something went wrong with the equation");
    }
late hedgeBOT
#

<@&987246399047479336> please have a look, thanks.

late hedgeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

a ScriptEngineManager
ScriptEngineManager manager = new ScriptEngineManager();

    // Get the JavaScript engine
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    
    // Define the math equation as a string
    String equation = "2 + 3 * 4";
    
    try {
        // Evaluate the math equation using the JavaScript engine
        Object result = engine.eval(equation);
        
        // Print the result
        System.out.println(result);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

}


Explanation:

1. Create a `ScriptEngineManager` object to manage script engines.
2. Use the `getEngineByName()` method of the `ScriptEngineManager` to get a specific script engine. In this case, we want to use the JavaScript engine, so we pass "JavaScript" as the argument.
3. Define your math equation as a string. In this example, we have "2 + 3 * 4".
4. Use the `eval()` method of the script engine to evaluate the math equation and store the result in an `Object` variable.
5. Print the result.

Make sure you have imported necessary classes like `javax.script.ScriptEngineManager` and `javax.script.ScriptException`.
pastel cargo
dull elbow
exotic coral
#

there exists libraries that can evaluate math expressions

pastel cargo
pastel cargo
dull elbow
twin mountainBOT
#
public class JShell implements AutoCloseable

The JShell evaluation state engine. This is the central class in the JShell API. A JShell instance holds the evolving compilation and execution state. The state is changed with the instance methods eval(String), drop(Snippet) and addToClasspath(String). The majority of methods query the state. A JShell instance also allows registering for events with onSnippetEvent(Consumer) and onShutdown(Consumer), which are unregistered with unsubscribe(Subscription). Access to the source analysis utilities is via #sourceCodeAnalysis. When complete the instance should be closed to free resources -- #close().

An instance of JShell is created with JShell.create().

This class is not thread safe, except as noted, all access should be through a single thread.

author

Robert Field

since

9

pastel cargo
#

well

#

problem is security

#

if you want to allow some unknown user to do math

#

here it's not just math that they can do

#

and this is a problem

#

same if you use js

pastel cargo