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));
Related
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.
This question already has answers here:
Pseudorandom Number Generator - Exponential Distribution
(9 answers)
Java exponential distribution
(1 answer)
Closed 5 years ago.
I am given a mean of 5. I need to generate a random number (exponentially distributed) in Java.
I know for Python, you can just run something like random.expovariate(5), but I'm not sure how to solve this for Java. Can anyone help me out?
see here. What you are looking for is something like this:
public double getNext() {
return Math.log(1-rand.nextDouble())/(-lambda);
}
(code taken from here)
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.
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 8 years ago.
so I am looking for advice on how to solve a calculation that is entered as a String in Java.
For example: "50 * 6 - 75 / 25" is entered I am then needing to solve this calculation using bodmas to.
So (50*6)-(75/25)
Answer = 297
I didn't explain it very well but any help would be appreciated.
You can use the BeanShell's Interpreter class to do this.
new Interpreter().eval("50 * 6 - 75 / 25");
Or you can also use the java's inbuilt JavaScript engine, an example of which is shown below
Evaluating a math expression given in string form
This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 8 years ago.
In C++, you can use __FUNCTION_NAME__ to get the name of the function that contains __FUNCTION_NAME__.
Is there an equivalent in Java? It could, in Java, be possible to do something with this and reflection. Is there something simpler though?
Thread.currentThread().getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html
Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();
depth = 0 (zero) will give current method
also
System.out.println((new Throwable()).getStackTrace()[0].toString());
Sample output:
com.junk.Junk3.main(Junk3.java:12)