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

  • Home
  • SEARCH
  • 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 8922299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:47:52+00:00 2026-06-15T06:47:52+00:00

Possible Duplicate: Convert String to code I need to evaluate a string containing valid

  • 0

Possible Duplicate:
Convert String to code

I need to evaluate a string containing valid Java code

eg. I should be able to get 6 from String code="Math.abs(2*3);";

  • 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-15T06:47:54+00:00Added an answer on June 15, 2026 at 6:47 am

    This sounds like quite an interesting idea, can I ask what the purpose of your application will be?

    The best bet is to build up a dictionary of known patterns you will support.

    My first idea is that you should create an ArrayList of accepted patterns. So for example:

    ArrayList<String> acceptedPatterns = new ArrayList<String>();
    acceptedPatterns.add("math.abs");
    acceptedPatterns.add("math.acos");
    

    etc.

    Then you can evaluate this list when you get hold of the string.

    String foundPattern = null;
    String myStringFromInput = editText.getText();
    for (String pattern : acceptedPatterns){
      if (myStringFromInput.contains(pattern){
         // we have recognised a java method, flag this up
         foundPattern = pattern;
         break;
      }
    }
    

    At this point you would have “Math.abs” in your foundPattern variable.

    You could then use your knowledge of how this method works to compute it. I can’t think of a super efficient way, but a basic way that would at least get you going would be something like a big if/else statement:

    int result = 0;
    if (foundPattern.contains("Math.abs"){
       result = computeMathAbs(myStringFromInput);
    }else if (foundPattern.contains("Math.acos"){
       // etc
    }
    

    And then in your computeMathAbs method you would do something like this:

    private int computeMathAbs(String input){
       int indexOfBracket = input.indexOf("(");
       int indexOfCloseBracket = input.indexOf(")");
       String value = input.subString(indexOfBracket,indexOfCloseBracket);
       int valueInt = computeFromString(value);
       int result = Math.abs(valueInt);
       return result;
    }
    

    You could then display the result passed back.

    The computeFromString() method will do a similar thing, looking for the * symbol or other symbols and turning this into a multiplication like the abs example.

    Obviously this is a basic example, so you would only be able to compute one java method at a time, the complexity for more than one method at a time would be quite difficult to program I think.

    You would also need to put quite a bit of error handling in, and recognise that the user might not enter perfect java. Can of worms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to convert from int to string in objective c: example code…
Possible Duplicate: how to convert java string to Date object In my Java code
Possible Duplicate: Convert String to code in Java Dynamic code execution on Java I
Possible Duplicate: Convert some code from C++ to C I've got some code that
Possible Duplicate: Simple way to repeat a String in java How can I convert
Possible Duplicate: how to convert java string to Date object I have a form
Possible Duplicate: Convert std::string to const char* or char* is there any to get
Possible Duplicate: How to get Color from Hex color code using .NET? I want
Possible Duplicate: Convert a String In C++ To Upper Case Hi, I need a
Possible Duplicate: Efficient Method for Creating CSV String from Lists/SortedLists C#? Here i need

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.