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 9124763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:38:24+00:00 2026-06-17T06:38:24+00:00

Given two classes A and B with one-argument constructors and a binary function f

  • 0

Given two classes A and B with one-argument constructors and a binary function f, I need to parse strings of the form

"f(A(10), B(20))"
"f(A(2), A(3))"

create the appropriate instances and call the respective functions:

class Functions {
    public static double f(A a1, A a2);
    public static double f(A a, B b);
    public static double f(B b1, B b2);
    // ...
}

So what I am looking for is a method of the kind

T createInstanceFromString(String desc);

to create the instances from strings like “A(10)” or “B(20)”. Of course and unfortunately, this is impossible in Java.

Any advice on solutions to this problem are greatly appreciated.

Edit: I forgot to mention that there is a potentially large number of different classes A, B, C, etc and a large number of functions f, g, etc. I’d like to use createInstanceFromString to separate the parsing from calling, otherwise one has to conditionally parse separately for each function call, which would bring a lot of code duplication.

Edit2: The question is not about how to parse, but about how to parse and then return an instance in a general purpose method/factory/whatever.

  • 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-17T06:38:25+00:00Added an answer on June 17, 2026 at 6:38 am

    You need to tokenize the String.
    (if it always starts with f( and ends with ) you can make your life easier, be removing these parts).

    A(4) -> Find the Classname -> A -> find the Parameter -> 4

    This is done like this (pseudo java code):

    public static Object createClass(String className, Double value){
        Class a = Class.forName(className);
        Class[] params = new Class[]{Integer.class};
        Constructor c = a.getConstructor(params);
        Object[] values = new Object[]{value};
        return c.newInstance();
    }
    

    Maybe, this lik is helpful: http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html

    update
    This is like writing a compiler. You can get further information, if you read something about, how to write a compiler: http://en.wikipedia.org/wiki/Compiler

    update 2
    put all generated instances into an array. Now you can get the correct method f with the reflections-API. The Code will be something like

    http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getDeclaredMethod(java.lang.String, java.lang.Class…)

    Object[] values ...
    Class[] classes ...
    for(Object o: values){
        add o.getClass() to classes
    }
    
    Method f = Functions.getClass().getDeclaredMethods("f", classes);
    f.invoke(values);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two classes, creating an object of each class in one another results in
Given: Two custom classes in Magento with a Many-to-One relationship between them. The child
I need to have two classes, one class has two methods each of which
Given two model classes, Foo and Bar , I want Foo to have 3
This is easier to explain with an example. Given these two classes: public class
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {
Given two identical boost::variant instances a and b , the expression ( a ==
Given two classes: First class performs AES encryption / decryption and returns encrypted /
Given two .Net types, type A and type B, how could one determine all
I have created two partial classes for a web page. Now i have given

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.