Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:51:39+00:00 2026-06-09T00:51:39+00:00

Java code… If I have int num = 10 + 5; System.out.println(num); The result

  • 0

Java code…

If I have

int num = 10 + 5;
System.out.println(num);

The result is 15

And if I have

String str = "10";
int number = Integer.parseInt(str);
System.out.println(number);

The result is 10

But if I have

String str = "10 + 5"; //here the problem.
int number = Integer.parseInt(str);
System.out.println(number);

The result is error message.

How to do it correctly. What I want to implement is to take expression from a String and calculate it. I am forced to take it from a String because I take it from JTextfield, so I want to make the calculation for the returned String expression.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:51:41+00:00Added an answer on June 9, 2026 at 12:51 am

    There is no simple way to do it directly because you need some sort of “expression evaluator”. One way would be to use a javascript engine from the scripting library:

    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
    
        String s = "10 + 5";
        int  result = ((Double) engine.eval(s)).intValue();
        System.out.println(result); // 15
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Java code is as follows: String s = 0.01; int i = Integer.parseInt(s);
I have some Java code: public class Car { private int yearModel; private String
Given this Java code: class Account { private Integer number = 0; public synchronized
I have the following Java code: String p = seven; String q = teen;
in my java code, I have a class with o member of type Map<String,String>
I have some Java code which sends out an email with code somewhat like
I have some Java code like int userid = take user input; And then
I have following Java code: import java.io.*; class Global{ public static int a =
My Java code must get string HH:MM from console and needs to operate with
I have a java code for SVG drawing. It processes transforms including rotate ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.