How to generate Java source code from strings? [duplicate] - java

This question already has answers here:
Is there an eval() function in Java?
(14 answers)
Closed 6 years ago.
I need to write a java program which reads rules (conditions) from a file. The rules have to be transformed as conditions in Java. I don't know how to transform the rules specified as strings to Java source code.
For example, the rules like:
x != y
or
n >= 0 && s == n
should be transformed to Java source code as:
if(x != y){......}
else{....}
How can I made this possible?

May be my answer looks like a cannon against sparrow, but look at JRule the engine that allows you create flexible system of business rules inside java application

Another option is Jess. I've used it in the past with good success for handling rules-based activities.

Related

Working in Eclipse IDE, values that should equal not equal [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am reasonably experienced with programming, but not specifically in Java. I am coming across the following error when working in eclipse. My code is the following:
I have used the debug function, and it reports that carbonPrefix is pent, but that carbon stays at 0 throughout. Like I said, I am a novice to Java and Eclipse, so I may not be using the debug function to it's full extent.
For anybody that's interested, this the start of code where you input the name of an alkane and it tells you the formula. It worked in Javascript and I'm just trying to translate it into Java.
Thank you all so much!
you have to use
carbonPrefix.equals("pent");
in java == operator used to compare two object references and the method equals() is used to compare two strings to determine whether they are equal or not.

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

How do I parse a Java file to retrieve its function names? [duplicate]

This question already has answers here:
Java : parse java source code, extract methods
(2 answers)
Closed 7 years ago.
I am working on a small program to compare two Java files. My goal is to compare the two files so that I can see what functions were added and deleted from one file to another (like a simple version control program). I am running into issues on how I should be handling these files. My current approach is to use a Scanner and use:
while(scanner.hasNext()) {
String function = scanner.next("((public|private|protected|static|final|native|synchronized|abstract|threadsafe|transient)+\\s)+[\\$_\\w\\<\\>\\[\\]]*\\s+[\\$_\\w]+\\([^\\)]*\\)?\\s*\\{?[^\\}]*\\}?");
System.out.println(function);
}
However this is not getting me any results for a file that I know has functions in it. Any tips or ideas on how to approach this?
You could use ANTLR Java grammar https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4 to get a full-blown Java parser and then use it to extract any information you need about Java files.

inserting c++ code in java [duplicate]

This question already has answers here:
Calling C++ functions from Java
(3 answers)
Closed 8 years ago.
Is there a way to insert some c++ code in java ?
for some reason my code which is:
ArRobotPacket pkt;
pkt.setID(ArCommands::SIM_SET_POSE);
pkt.uByteToBuf(0); // argument type: ignored.
pkt.byte4ToBuf(x);
pkt.byte4ToBuf(y);
pkt.byte4ToBuf(th);
pkt.finalizePacket();
robot.getDeviceConnection()->write(pkt.getBuf(), pkt.getLength());
translated to java, will not function, the write will actually send a packet but doesn't effect the program the way it should
This code is from
http://robots.mobilerobots.com/MobileSim/download/current/README.html#mapobjs
One way is to use Java Native Interface: http://docs.oracle.com/javase/6/docs/technotes/guides/jni/

# in java string instead of backslashes [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Raw Strings in Java?
In C# there is such a thing as # ("at sign") that can be put before string if forbidden symbols occurs. For example:
#"a\b\c"
In java I have to put backslashes
"a\\b\\c"
Is there any way in Java to make this easier?
Another way may be use equvivalent code for the symbols you want to escape.
Not really. I have made the transition not long ago and at first was constantly looking for "what is C#'s equivalent in Java for xyz?"
This is sometimes helpful but mostly frustrating. C# is a much more advanced language than Java and it will take a long time for Java to catch up.
You get used to it over time :-)

Categories

Resources