How to convert string in a txt document to code? [duplicate] - java
I have a string like the following:
String str = "4*5";
Now I have to get the result of 20 by using the string.
I know in some other languages the eval() function will do this.
How can I do this in Java?
You can use the ScriptEngine class and evaluate it as a Javascript string.
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval("4*5");
There may be a better way, but this one works.
There is no standard Java class or method that will do what you want. Your options include:
Select and use some third-party expression evaluation library. For example JEL or any of the half dozen libraries listed here.
Wrap the expression in the Java source code for a class with an eval method, send that to the Java compiler, and then load the resulting compiled class.
Use some scripting language that can be called from Java as an expression evaluator. Possibilities include Javascript1, BeanShell, and so on. A JSR 223 compliant scripting language implementation can be called via the Scripting API.
Write your own expression evaluator from scratch.
The first approach is probably simplest. The second and third approaches are a potential security risk if you get the expression to be evaluated from an untrusted user. (Think code injection.)
1 - Javascript in Java SE is a moving target. From Java 6, a version of Mozilla's Rhino Javascript implementation was bundled with Java SE. The in Java 8, it was superseded by Nashorn. In Java 11, Nashorn was deprecated, and finally dropped from the core codebase. As of 2021, both Rhino and Nashorn are being maintained as separate (non-Oracle) products, and Oracle's GraalVM has its own Javascript implementation.
There are very few real use cases in which being able to evaluate a String as a fragment of Java code is necessary or desirable. That is, asking how to do this is really an XY problem: you actually have a different problem, which can be solved a different way.
First ask yourself, where did this String that you wish to evaluate come from? Did another part of your program generate it, or was it input provided by the user?
Another part of my program generated it: so, you want one part of your program to decide the kind of operation to perform, but not perform the operation, and a second part that performs the chosen operation. Instead of generating and then evaluating a String, use the Strategy, Command or Builder design pattern, as appropriate for your particular case.
It is user input: the user could input anything, including commands that, when executed, could cause your program to misbehave, crash, expose information that should be secret, damage persistent information (such as the content of a database), and other such nastiness. The only way to prevent that would be to parse the String yourself, check it was not malicious, and then evaluate it. But parsing it yourself is much of the work that the requested evalfunction would do, so you have saved yourself nothing. Worse still, checking that arbitrary Java was not malicious is impossible, because checking that is the halting problem.
It is user input, but the syntax and semantics of permitted text to evaluate is greatly restricted: No general purpose facility can easily implement a general purpose parser and evaluator for whatever restricted syntax and semantics you have chosen. What you need to do is implement a parser and evaluator for your chosen syntax and semantics. If the task is simple, you could write a simple recursive-descent or finite-state-machine parser by hand. If the task is difficult, you could use a compiler-compiler (such as ANTLR) to do some of the work for you.
I just want to implement a desktop calculator!: A homework assignment, eh? If you could implement the evaluation of the input expression using a provided eval function, it would not be much of a homework assignment, would it? Your program would be three lines long. Your instructor probably expects you to write the code for a simple arithmetic parser/evaluator. There is well known algorithm, shunting-yard, which you might find useful.
With Java 9, we get access to jshell, so one can write something like this:
import jdk.jshell.JShell;
import java.lang.StringBuilder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Eval {
public static void main(String[] args) throws IOException {
try(JShell js = JShell.create(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
js.onSnippetEvent(snip -> {
if (snip.status() == jdk.jshell.Snippet.Status.VALID) {
System.out.println("➜ " + snip.value());
}
});
System.out.print("> ");
for (String line = br.readLine(); line != null; line = br.readLine()) {
js.eval(js.sourceCodeAnalysis().analyzeCompletion(line).source());
System.out.print("> ");
}
}
}
}
Sample run:
> 1 + 2 / 4 * 3
➜ 1
> 32 * 121
➜ 3872
> 4 * 5
➜ 20
> 121 * 51
➜ 6171
>
Slightly op, but that's what Java currently has to offer
I could advise you to use Exp4j. It is easy to understand as you can see from the following example code:
Expression e = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)")
.variables("x", "y")
.build()
.setVariable("x", 2.3)
.setVariable("y", 3.14);
double result = e.evaluate();
No, you can not have a generic "eval" in Java (or any compiled language). Unless you're willing to write a Java compiler AND a JVM to be executed inside of your Java program.
Yes, you can have some library to evaluate numeric algebraic expressions like the one above - see this thread for discussion.
As previous answers, there is no standard API in Java for this.
You can add groovy jar files to your path and groovy.util.Eval.me("4*5") gets your job done.
A fun way to solve your problem could be coding an eval() function on your own!
I've done it for you!
You can use FunctionSolver library simply by typing FunctionSolver.solveByX(function,value) inside your code. The function attribute is a String which represents the function you want to solve, the value attribute is the value of the independent variable
of your function (which MUST be x).
If you want to solve a function which contains more than one independent variable, you can use FunctionSolver.solve(function,values) where the values attribute is an HashMap(String,Double) which contains all your independent attributes (as Strings) and their respective values (as Doubles).
Another piece of information: I've coded a simple version of FunctionSolver, so its supports only Math methods which return a double value and which accepts one or two double values as fields (just use FunctionSolver.usableMathMethods() if you're curious) (These methods are: bs, sin, cos, tan, atan2, sqrt, log, log10, pow, exp, min, max, copySign, signum, IEEEremainder, acos, asin, atan, cbrt, ceil, cosh, expm1, floor, hypot, log1p, nextAfter, nextDown, nextUp, random, rint, sinh, tanh, toDegrees, toRadians, ulp). Also, that library supports the following operators: * / + - ^ (even if java normally does not support the ^ operator).
One last thing: while creating this library I had to use reflections to call Math methods. I think it's really cool, just have a look at this if you are interested in!
That's all, here it is the code (and the library):
package core;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public abstract class FunctionSolver {
public static double solveNumericExpression (String expression) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return solve(expression, new HashMap<>());
}
public static double solveByX (String function, double value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
HashMap<String, Double> values = new HashMap<>();
values.put("x", value);
return solveComplexFunction(function, function, values);
}
public static double solve (String function, HashMap<String,Double> values) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return solveComplexFunction(function, function, values);
}
private static double solveComplexFunction (String function, String motherFunction, HashMap<String, Double> values) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
int position = 0;
while(position < function.length()) {
if (alphabetic.contains(""+function.charAt(position))) {
if (position == 0 || !alphabetic.contains(""+function.charAt(position-1))) {
int endIndex = -1;
for (int j = position ; j < function.length()-1 ; j++) {
if (alphabetic.contains(""+function.charAt(j))
&& !alphabetic.contains(""+function.charAt(j+1))) {
endIndex = j;
break;
}
}
if (endIndex == -1 & alphabetic.contains(""+function.charAt(function.length()-1))) {
endIndex = function.length()-1;
}
if (endIndex != -1) {
String alphabeticElement = function.substring(position, endIndex+1);
if (Arrays.asList(usableMathMethods()).contains(alphabeticElement)) {
//Start analyzing a Math function
int closeParenthesisIndex = -1;
int openedParenthesisquantity = 0;
int commaIndex = -1;
for (int j = endIndex+1 ; j < function.length() ; j++) {
if (function.substring(j,j+1).equals("(")) {
openedParenthesisquantity++;
}else if (function.substring(j,j+1).equals(")")) {
openedParenthesisquantity--;
if (openedParenthesisquantity == 0) {
closeParenthesisIndex = j;
break;
}
}else if (function.substring(j,j+1).equals(",") & openedParenthesisquantity == 0) {
if (commaIndex == -1) {
commaIndex = j;
}else{
throw new IllegalArgumentException("The argument of math function (which is "+alphabeticElement+") has too many commas");
}
}
}
if (closeParenthesisIndex == -1) {
throw new IllegalArgumentException("The argument of a Math function (which is "+alphabeticElement+") hasn't got the closing bracket )");
}
String functionArgument = function.substring(endIndex+2,closeParenthesisIndex);
if (commaIndex != -1) {
double firstParameter = solveComplexFunction(functionArgument.substring(0,commaIndex),motherFunction,values);
double secondParameter = solveComplexFunction(functionArgument.substring(commaIndex+1),motherFunction,values);
Method mathMethod = Math.class.getDeclaredMethod(alphabeticElement, new Class<?>[] {double.class, double.class});
mathMethod.setAccessible(true);
String newKey = getNewKey(values);
values.put(newKey, (Double) mathMethod.invoke(null, firstParameter, secondParameter));
function = function.substring(0, position)+newKey
+((closeParenthesisIndex == function.length()-1)?(""):(function.substring(closeParenthesisIndex+1)));
}else {
double firstParameter = solveComplexFunction(functionArgument, motherFunction, values);
Method mathMethod = Math.class.getDeclaredMethod(alphabeticElement, new Class<?>[] {double.class});
mathMethod.setAccessible(true);
String newKey = getNewKey(values);
values.put(newKey, (Double) mathMethod.invoke(null, firstParameter));
function = function.substring(0, position)+newKey
+((closeParenthesisIndex == function.length()-1)?(""):(function.substring(closeParenthesisIndex+1)));
}
}else if (!values.containsKey(alphabeticElement)) {
throw new IllegalArgumentException("Found a group of letters ("+alphabeticElement+") which is neither a variable nor a Math function: ");
}
}
}
}
position++;
}
return solveBracketsFunction(function,motherFunction,values);
}
private static double solveBracketsFunction (String function,String motherFunction,HashMap<String, Double> values) throws IllegalArgumentException{
function = function.replace(" ", "");
String openingBrackets = "([{";
String closingBrackets = ")]}";
int parenthesisIndex = 0;
do {
int position = 0;
int openParenthesisBlockIndex = -1;
String currentOpeningBracket = openingBrackets.charAt(parenthesisIndex)+"";
String currentClosingBracket = closingBrackets.charAt(parenthesisIndex)+"";
if (contOccouranceIn(currentOpeningBracket,function) != contOccouranceIn(currentClosingBracket,function)) {
throw new IllegalArgumentException("Error: brackets are misused in the function "+function);
}
while (position < function.length()) {
if (function.substring(position,position+1).equals(currentOpeningBracket)) {
if (position != 0 && !operators.contains(function.substring(position-1,position))) {
throw new IllegalArgumentException("Error in function: there must be an operator following a "+currentClosingBracket+" breacket");
}
openParenthesisBlockIndex = position;
}else if (function.substring(position,position+1).equals(currentClosingBracket)) {
if (position != function.length()-1 && !operators.contains(function.substring(position+1,position+2))) {
throw new IllegalArgumentException("Error in function: there must be an operator before a "+currentClosingBracket+" breacket");
}
String newKey = getNewKey(values);
values.put(newKey, solveBracketsFunction(function.substring(openParenthesisBlockIndex+1,position),motherFunction, values));
function = function.substring(0,openParenthesisBlockIndex)+newKey
+((position == function.length()-1)?(""):(function.substring(position+1)));
position = -1;
}
position++;
}
parenthesisIndex++;
}while (parenthesisIndex < openingBrackets.length());
return solveBasicFunction(function,motherFunction, values);
}
private static double solveBasicFunction (String function, String motherFunction, HashMap<String, Double> values) throws IllegalArgumentException{
if (!firstContainsOnlySecond(function, alphanumeric+operators)) {
throw new IllegalArgumentException("The function "+function+" is not a basic function");
}
if (function.contains("**") |
function.contains("//") |
function.contains("--") |
function.contains("+*") |
function.contains("+/") |
function.contains("-*") |
function.contains("-/")) {
/*
* ( -+ , +- , *- , *+ , /- , /+ )> Those values are admitted
*/
throw new IllegalArgumentException("Operators are misused in the function");
}
function = function.replace(" ", "");
int position;
int operatorIndex = 0;
String currentOperator;
do {
currentOperator = operators.substring(operatorIndex,operatorIndex+1);
if (currentOperator.equals("*")) {
currentOperator+="/";
operatorIndex++;
}else if (currentOperator.equals("+")) {
currentOperator+="-";
operatorIndex++;
}
operatorIndex++;
position = 0;
while (position < function.length()) {
if ((position == 0 && !(""+function.charAt(position)).equals("-") && !(""+function.charAt(position)).equals("+") && operators.contains(""+function.charAt(position))) ||
(position == function.length()-1 && operators.contains(""+function.charAt(position)))){
throw new IllegalArgumentException("Operators are misused in the function");
}
if (currentOperator.contains(function.substring(position, position+1)) & position != 0) {
int firstTermBeginIndex = position;
while (firstTermBeginIndex > 0) {
if ((alphanumeric.contains(""+function.charAt(firstTermBeginIndex))) & (operators.contains(""+function.charAt(firstTermBeginIndex-1)))){
break;
}
firstTermBeginIndex--;
}
if (firstTermBeginIndex != 0 && (function.charAt(firstTermBeginIndex-1) == '-' | function.charAt(firstTermBeginIndex-1) == '+')) {
if (firstTermBeginIndex == 1) {
firstTermBeginIndex--;
}else if (operators.contains(""+(function.charAt(firstTermBeginIndex-2)))){
firstTermBeginIndex--;
}
}
String firstTerm = function.substring(firstTermBeginIndex,position);
int secondTermLastIndex = position;
while (secondTermLastIndex < function.length()-1) {
if ((alphanumeric.contains(""+function.charAt(secondTermLastIndex))) & (operators.contains(""+function.charAt(secondTermLastIndex+1)))) {
break;
}
secondTermLastIndex++;
}
String secondTerm = function.substring(position+1,secondTermLastIndex+1);
double result;
switch (function.substring(position,position+1)) {
case "*": result = solveSingleValue(firstTerm,values)*solveSingleValue(secondTerm,values); break;
case "/": result = solveSingleValue(firstTerm,values)/solveSingleValue(secondTerm,values); break;
case "+": result = solveSingleValue(firstTerm,values)+solveSingleValue(secondTerm,values); break;
case "-": result = solveSingleValue(firstTerm,values)-solveSingleValue(secondTerm,values); break;
case "^": result = Math.pow(solveSingleValue(firstTerm,values),solveSingleValue(secondTerm,values)); break;
default: throw new IllegalArgumentException("Unknown operator: "+currentOperator);
}
String newAttribute = getNewKey(values);
values.put(newAttribute, result);
function = function.substring(0,firstTermBeginIndex)+newAttribute+function.substring(secondTermLastIndex+1,function.length());
deleteValueIfPossible(firstTerm, values, motherFunction);
deleteValueIfPossible(secondTerm, values, motherFunction);
position = -1;
}
position++;
}
}while (operatorIndex < operators.length());
return solveSingleValue(function, values);
}
private static double solveSingleValue (String singleValue, HashMap<String, Double> values) throws IllegalArgumentException{
if (isDouble(singleValue)) {
return Double.parseDouble(singleValue);
}else if (firstContainsOnlySecond(singleValue, alphabetic)){
return getValueFromVariable(singleValue, values);
}else if (firstContainsOnlySecond(singleValue, alphanumeric+"-+")) {
String[] composition = splitByLettersAndNumbers(singleValue);
if (composition.length != 2) {
throw new IllegalArgumentException("Wrong expression: "+singleValue);
}else {
if (composition[0].equals("-")) {
composition[0] = "-1";
}else if (composition[1].equals("-")) {
composition[1] = "-1";
}else if (composition[0].equals("+")) {
composition[0] = "+1";
}else if (composition[1].equals("+")) {
composition[1] = "+1";
}
if (isDouble(composition[0])) {
return Double.parseDouble(composition[0])*getValueFromVariable(composition[1], values);
}else if (isDouble(composition[1])){
return Double.parseDouble(composition[1])*getValueFromVariable(composition[0], values);
}else {
throw new IllegalArgumentException("Wrong expression: "+singleValue);
}
}
}else {
throw new IllegalArgumentException("Wrong expression: "+singleValue);
}
}
private static double getValueFromVariable (String variable, HashMap<String, Double> values) throws IllegalArgumentException{
Double val = values.get(variable);
if (val == null) {
throw new IllegalArgumentException("Unknown variable: "+variable);
}else {
return val;
}
}
/*
* FunctionSolver help tools:
*
*/
private static final String alphabetic = "abcdefghilmnopqrstuvzwykxy";
private static final String numeric = "0123456789.";
private static final String alphanumeric = alphabetic+numeric;
private static final String operators = "^*/+-"; //--> Operators order in important!
private static boolean firstContainsOnlySecond(String firstString, String secondString) {
for (int j = 0 ; j < firstString.length() ; j++) {
if (!secondString.contains(firstString.substring(j, j+1))) {
return false;
}
}
return true;
}
private static String getNewKey (HashMap<String, Double> hashMap) {
String alpha = "abcdefghilmnopqrstuvzyjkx";
for (int j = 0 ; j < alpha.length() ; j++) {
String k = alpha.substring(j,j+1);
if (!hashMap.containsKey(k) & !Arrays.asList(usableMathMethods()).contains(k)) {
return k;
}
}
for (int j = 0 ; j < alpha.length() ; j++) {
for (int i = 0 ; i < alpha.length() ; i++) {
String k = alpha.substring(j,j+1)+alpha.substring(i,i+1);
if (!hashMap.containsKey(k) & !Arrays.asList(usableMathMethods()).contains(k)) {
return k;
}
}
}
throw new NullPointerException();
}
public static String[] usableMathMethods () {
/*
* Only methods that:
* return a double type
* present one or two parameters (which are double type)
*/
Method[] mathMethods = Math.class.getDeclaredMethods();
ArrayList<String> usableMethodsNames = new ArrayList<>();
for (Method method : mathMethods) {
boolean usable = true;
int argumentsCounter = 0;
Class<?>[] methodParametersTypes = method.getParameterTypes();
for (Class<?> parameter : methodParametersTypes) {
if (!parameter.getSimpleName().equalsIgnoreCase("double")) {
usable = false;
break;
}else {
argumentsCounter++;
}
}
if (!method.getReturnType().getSimpleName().toLowerCase().equals("double")) {
usable = false;
}
if (usable & argumentsCounter<=2) {
usableMethodsNames.add(method.getName());
}
}
return usableMethodsNames.toArray(new String[usableMethodsNames.size()]);
}
private static boolean isDouble (String number) {
try {
Double.parseDouble(number);
return true;
}catch (Exception ex) {
return false;
}
}
private static String[] splitByLettersAndNumbers (String val) {
if (!firstContainsOnlySecond(val, alphanumeric+"+-")) {
throw new IllegalArgumentException("Wrong passed value: <<"+val+">>");
}
ArrayList<String> response = new ArrayList<>();
String searchingFor;
int lastIndex = 0;
if (firstContainsOnlySecond(""+val.charAt(0), numeric+"+-")) {
searchingFor = alphabetic;
}else {
searchingFor = numeric+"+-";
}
for (int j = 0 ; j < val.length() ; j++) {
if (searchingFor.contains(val.charAt(j)+"")) {
response.add(val.substring(lastIndex, j));
lastIndex = j;
if (searchingFor.equals(numeric+"+-")) {
searchingFor = alphabetic;
}else {
searchingFor = numeric+"+-";
}
}
}
response.add(val.substring(lastIndex,val.length()));
return response.toArray(new String[response.size()]);
}
private static void deleteValueIfPossible (String val, HashMap<String, Double> values, String function) {
if (values.get(val) != null & function != null) {
if (!function.contains(val)) {
values.remove(val);
}
}
}
private static int contOccouranceIn (String howManyOfThatString, String inThatString) {
return inThatString.length() - inThatString.replace(howManyOfThatString, "").length();
}
}
Writing your own library is not that hard as u might thing. Here is link for Shunting-yard algorithm with step by step algorithm explenation. Although, you will have to parse the input for tokens first.
There are 2 other questions wich can give you some information too:
Turn a String into a Math Expression?
What's a good library for parsing mathematical expressions in java?
As there are many answers, I'm adding my implementation on top of eval() method with some additional features like support for factorial, evaluating complex expressions etc.
package evaluation;
import java.math.BigInteger;
import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class EvalPlus {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("This Evaluation is based on BODMAS rule\n");
evaluate();
}
private static void evaluate() {
StringBuilder finalStr = new StringBuilder();
System.out.println("Enter an expression to evaluate:");
String expr = scanner.nextLine();
if(isProperExpression(expr)) {
expr = replaceBefore(expr);
char[] temp = expr.toCharArray();
String operators = "(+-*/%)";
for(int i = 0; i < temp.length; i++) {
if((i == 0 && temp[i] != '*') || (i == temp.length-1 && temp[i] != '*' && temp[i] != '!')) {
finalStr.append(temp[i]);
} else if((i > 0 && i < temp.length -1) || (i==temp.length-1 && temp[i] == '!')) {
if(temp[i] == '!') {
StringBuilder str = new StringBuilder();
for(int k = i-1; k >= 0; k--) {
if(Character.isDigit(temp[k])) {
str.insert(0, temp[k] );
} else {
break;
}
}
Long prev = Long.valueOf(str.toString());
BigInteger val = new BigInteger("1");
for(Long j = prev; j > 1; j--) {
val = val.multiply(BigInteger.valueOf(j));
}
finalStr.setLength(finalStr.length() - str.length());
finalStr.append("(" + val + ")");
if(temp.length > i+1) {
char next = temp[i+1];
if(operators.indexOf(next) == -1) {
finalStr.append("*");
}
}
} else {
finalStr.append(temp[i]);
}
}
}
expr = finalStr.toString();
if(expr != null && !expr.isEmpty()) {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
try {
System.out.println("Result: " + engine.eval(expr));
evaluate();
} catch (ScriptException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Please give an expression");
evaluate();
}
} else {
System.out.println("Not a valid expression");
evaluate();
}
}
private static String replaceBefore(String expr) {
expr = expr.replace("(", "*(");
expr = expr.replace("+*", "+").replace("-*", "-").replace("**", "*").replace("/*", "/").replace("%*", "%");
return expr;
}
private static boolean isProperExpression(String expr) {
expr = expr.replaceAll("[^()]", "");
char[] arr = expr.toCharArray();
Stack<Character> stack = new Stack<Character>();
int i =0;
while(i < arr.length) {
try {
if(arr[i] == '(') {
stack.push(arr[i]);
} else {
stack.pop();
}
} catch (EmptyStackException e) {
stack.push(arr[i]);
}
i++;
}
return stack.isEmpty();
}
}
Please find the updated gist anytime here. Also comment if any issues are there. Thanks.
There are some perfectly capable answers here. However for non-trivial script it may be desirable to retain the code in a cache, or for debugging purposes, or even to have dynamically self-updating code.
To that end, sometimes it's simpler or more robust to interact with Java via command line. Create a temporary directory, output your script and any assets, create the jar. Finally import your new code.
It's a bit beyond the scope of normal eval() use in most languages, though you could certainly implement eval by returning the result from some function in your jar.
Still, thought I'd mention this method as it does fully encapsulate everything Java can do without 3rd party tools, in case of desperation. This method allows me to turn HTML templates into objects and save them, avoiding the need to parse a template at runtime.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
class Calculate {
public static void main(String[] args) {
String strng = "8*-2*3*-1*10/2+6-2";
String[] oparator = {"+","-","*","/"};
List<String> op1 = new ArrayList<>();
String[] x = strng.split("");
int sayac=0;
for (String i : x) {
sayac ++;
for (String c : oparator) {
if (i.equals(c)) {
try {
int j = Integer.parseInt(strng.substring(0, sayac - 1));
op1.add(strng.substring(0, sayac - 1));
op1.add(c);
strng = strng.substring(sayac);
sayac = 0;
}catch (Exception e)
{
continue;
}
}
}
}
op1.add(strng);
ListIterator<String> it = op1.listIterator();
List<List> newlist = new ArrayList<>() ;
while (it.hasNext()) {
List<String> p= new ArrayList<>();
p.add(String.valueOf(it.nextIndex()));
p.add(it.next());
newlist.add(p);
}
int sayac2=0;
String oparatorvalue = "*";
calculate(sayac2,newlist,oparatorvalue);
String oparatorvalue2 = "/";
calculate(sayac2,newlist,oparatorvalue2);
String oparatorvalue3 = "+";
calculate(sayac2,newlist,oparatorvalue3);
String oparatorvalue4 = "-";
calculate(sayac2,newlist,oparatorvalue4);
System.out.println("Result:"+newlist.get(0).get(1));
}
private static void calculate(int sayac2, List<List> newlist, String oparatorvalue) {
while (sayac2<4){
try{
for (List j : newlist) {
if (j.get(1) == oparatorvalue) {
Integer opindex = newlist.indexOf(j);
Object sayi1 = newlist.get(opindex - 1).get(1);
Object sayi2 = newlist.get(opindex + 1).get(1);
int sonuc=0;
if (oparatorvalue.equals("*")){
sonuc = Integer.parseInt(sayi1.toString()) * Integer.parseInt(sayi2.toString());
}
if (oparatorvalue.equals("/")){
sonuc = Integer.parseInt(sayi1.toString()) / Integer.parseInt(sayi2.toString());
}
if (oparatorvalue.equals("+")){
sonuc = Integer.parseInt(sayi1.toString()) + Integer.parseInt(sayi2.toString());
}
if (oparatorvalue.equals("-")){
sonuc = Integer.parseInt(sayi1.toString()) - Integer.parseInt(sayi2.toString());
}
newlist.remove(opindex - 1);
newlist.remove(opindex - 1);
newlist.remove(opindex - 1);
List<String> sonuclist = new ArrayList<>();
sonuclist.add(String.valueOf(opindex - 1));
sonuclist.add(String.valueOf(sonuc));
newlist.add(opindex - 1, sonuclist);
}}}
catch (Exception e){
continue;
}
sayac2++;}
}
}
If you do not want to import heavy scripting library, you can use SimpleExpressionEvaluator directly into your code
Usage:
Expression.eval("1+2").asString(); // returns "3.0"
Expression.eval("1+2").asInt(); // returns 3
Expression.eval("2>3").asString(); // returns "false"
Expression.eval("2>3").asBoolean(); // returns false
Expression.eval("(3>2)||((2<4)&&(2>1))").asString(); // returns "true"
With variables:
HashMap<String, Object> st = new HashMap<String, Object>();
st.put("a",1);
st.put("b",2);
st.put("c",3);
st.put("d",4);
Expression.eval("a+b", st).asInt(); // or simply asString()
Expression.eval("a>b",st).asBoolean(); // or simply asString()
Expression.eval("(c>b)||((b<d)&&(b>a))",st).asBoolean(); // or simply asString()
Expression.eval("(c>2)||((2<d)&&(b>1))",st).asBoolean(); // or simply asString()
Using ExpressionBuilder:
Expression.expressionBuilder().putSymbol("a",2).putSymbol("b",3).build("(b>a)").evaluate()
The following resolved the issue:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String str = "4*5";
System.out.println(engine.eval(str));
Related
Finding the depth of a JSON String in JAVA
I was given a String in java method and have to return the son depth of that string like if the method is find depthJSON(String s){ //code return count; } for example: { "0" : { "name" : "John", "City" : "NY"}, "1" : { "name" : "Mike", "City" : "LA"} } for this input, depth is 2
Your problem calls for a JSON Tokenizer. You do not actually need to parse your file ( ie. build some tree structure as a Java object ) as you are only interested in the maximal nesting depth which can be deduced from the token stream alone. The basic idea is to keep tabs on the tokens that represent the start and end of an object ( {, } ). Whenever the tokenizer sees an object start/end, increment/decrement a global depth counter; Whenever the tokenizer sees an object end, check whether the current global depth counter is greater than the maximum seen so far and change the value accordingly. As a package to furnish the JSON Tokenizer, I'd suggest Jackson. Download the jar files into your source directory (or to the suitable place in your development toolset directory). The jar files are available at: https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.10.3/jackson-core-2.10.3.jar https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.10.3/jackson-annotations-2.10.3.jar https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.10.3/jackson-databind-2.10.3.jar Note that you need all of them and do not forget to alter the Java CLASSPATH unless your IDE takes care of that. Code The following class determines and prints the depth of your json: // https://stackoverflow.com/questions/61097605/finding-the-depth-of-a-json-string-in-java // // javac.exe -cp "./jackson-core-2.10.3.jar;./jackson-annotations-2.10.3.jar;./jackson-databind-2.10.3.jar" .\json_nesting.java // java.exe -cp "./jackson-core-2.10.3.jar;./jackson-annotations-2.10.3.jar;./jackson-databind-2.10.3.jar;." json_nesting // // Cf. https://github.com/FasterXML/jackson-databind#5-minute-tutorial-streaming-parser-generator // // Jar downloads // https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.10.3/jackson-core-2.10.3.jar // https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.10.3/jackson-annotations-2.10.3.jar // https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.10.3/jackson-databind-2.10.3.jar // import java.lang.*; import java.io.*; import java.nio.*; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.ObjectMapper; public class json_nesting { public static void main(String[] args) { // write your code here // Second: read file back String fpath = "<file path goes here>"; // Double backslashes on Windows ! ObjectMapper mapper = new ObjectMapper(); // create once, reuse JsonFactory f = mapper.getFactory(); try { JsonParser p = f.createParser(new File(fpath)); JsonToken t = p.nextToken(); int currentDepth = 0; int maxDepth = 0; while (t != null) { if (t == JsonToken.START_OBJECT) { currentDepth++; } else { if (t == JsonToken.END_OBJECT) { if (currentDepth > maxDepth) { maxDepth = currentDepth; } currentDepth--; } } t = p.nextToken(); }; System.out.printf("Max depth = %4d.\n", maxDepth); p.close(); } catch ( IOException ioe ) { System.out.printf("File processing failed: = '%s'. Message: %s.\n", fpath, ioe.getMessage()); } } } Tested with Java SE 11, u 28. Note (JSON syntax) Note that your sample file is NOT valid JSON - some property names are not enclosed in double quotes, properties are not separated with commas. A rectified version comes here: { "0" : { "name" : "John", "City" : "NY"} , "1" : { "name" : "Mike", "City" : "LA"} }
public static int levelCount(String word) { int current= 0; int max = 0; for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == '{') { current++; } else { if (word.charAt(i) == '}') { if (current > max) { max = current; } current--; } } } return max; }
my answer is int JsonDepth (String S) { int current_max = 0; int max = 0; int n = S.length(); for (int i = 0; i < n; i++) { if (S.charAt(i) == '{') { current_max++; // update max if required if (current_max > max) { max = current_max; } } else if (S.charAt(i) == '}') { if (current_max > 0) { current_max--; } else { return -1; } } } if (current_max != 0) { return -1; } return max; }
antlr4/java: pretty print parse tree to stdout
Beginners question: how do I print a readable version of the parse tree to stdout? CharStream input = CharStreams.fromFileName("testdata/test.txt"); MyLexer lexer = new MyLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); MyParser parser = new MyParser(tokens); parser.setBuildParseTree(true); RuleContext tree = parser.record(); System.out.println(tree.toStringTree(parser)); this prints the whole tree on a single line delimited by brackets '()'. (record (husband <4601> (name KOHAI Nikolaus) \n (birth * um.1872 (place Ploschitz)) \n\n) (wife (marriage oo) \n (name SCHLOTTHAUER Maria) \n (birth * um.1877 ... I would like to have something like this record husband <id> name <name> ... wife
Extracted from SnippetsTest as a standalone utility class: import java.util.List; import org.antlr.v4.runtime.misc.Utils; import org.antlr.v4.runtime.tree.Tree; import org.antlr.v4.runtime.tree.Trees; public class TreeUtils { /** Platform dependent end-of-line marker */ public static final String Eol = System.lineSeparator(); /** The literal indent char(s) used for pretty-printing */ public static final String Indents = " "; private static int level; private TreeUtils() {} /** * Pretty print out a whole tree. {#link #getNodeText} is used on the node payloads to get the text * for the nodes. (Derived from Trees.toStringTree(....)) */ public static String toPrettyTree(final Tree t, final List<String> ruleNames) { level = 0; return process(t, ruleNames).replaceAll("(?m)^\\s+$", "").replaceAll("\\r?\\n\\r?\\n", Eol); } private static String process(final Tree t, final List<String> ruleNames) { if (t.getChildCount() == 0) return Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false); StringBuilder sb = new StringBuilder(); sb.append(lead(level)); level++; String s = Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false); sb.append(s + ' '); for (int i = 0; i < t.getChildCount(); i++) { sb.append(process(t.getChild(i), ruleNames)); } level--; sb.append(lead(level)); return sb.toString(); } private static String lead(int level) { StringBuilder sb = new StringBuilder(); if (level > 0) { sb.append(Eol); for (int cnt = 0; cnt < level; cnt++) { sb.append(Indents); } } return sb.toString(); } } Call the method as follows: List<String> ruleNamesList = Arrays.asList(parser.getRuleNames()); String prettyTree = TreeUtils.toPrettyTree(tree, ruleNamesList);
Besides a graphical parse tree my ANTLR4 extension for Visual Studio Code also produces a formatted text parse tree:
If you like to use regex only for what it's really for, you can always print a tree by yourself: import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.Trees; public static String printSyntaxTree(Parser parser, ParseTree root) { StringBuilder buf = new StringBuilder(); recursive(root, buf, 0, Arrays.asList(parser.getRuleNames())); return buf.toString(); } private static void recursive(ParseTree aRoot, StringBuilder buf, int offset, List<String> ruleNames) { for (int i = 0; i < offset; i++) { buf.append(" "); } buf.append(Trees.getNodeText(aRoot, ruleNames)).append("\n"); if (aRoot instanceof ParserRuleContext) { ParserRuleContext prc = (ParserRuleContext) aRoot; if (prc.children != null) { for (ParseTree child : prc.children) { recursive(child, buf, offset + 1, ruleNames); } } } } Usage: ParseTree root = parser.yourOwnRule(); System.out.println(printSyntaxTree(parser, root));
I wanted to put in my own spin on this, taking advantage of the fact that I already use StringTemplate in my project. This means I don't have to manually deal with levels like the other answers. It also makes the output format easier to customize. On top of that, the main reason I'm posting this is because I decided to skip printing rules that I'm only 'passing through', i.e. when using chain rules a : b | something_else ; b : c | another ; c : d | yet_more ; d : rule that matters ; since they cluttered my output when checking trees from small inputs without adding any usefull information. This is also easy to change, at the //pass-through rules comment location. I also copied in the definition of Trees.getNodeText and modified it to use a plain array to get rid of the unnecessary wrapping, and even let me customize it if I feel like it. Finally, I made it take the parser and tree and just straight dump to System.out, since that's the only situation I need it in. import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.RuleContext; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.Tree; import org.stringtemplate.v4.ST; //for pretty-dumping trees in short form public class TreeUtils { private static final ST template() { return new ST("<rule_text>\n\t<child; separator=\"\n\">"); } private static final ST literal(String text) { return new ST("<text>").add("text", text); } public static void dump(Parser parser, Tree tree) { System.out.println(process(parser.getRuleNames(),tree).render()); } private static String getNodeText(Tree t, String[] ruleNames) { if ( t instanceof RuleContext ) { int ruleIndex = ((RuleContext)t).getRuleContext().getRuleIndex(); String ruleName = ruleNames[ruleIndex]; return ruleName; } else if ( t instanceof ErrorNode) { return t.toString(); } else if ( t instanceof TerminalNode) { Token symbol = ((TerminalNode)t).getSymbol(); if (symbol != null) { String s = symbol.getText(); return s; } } Object payload = t.getPayload(); if ( payload instanceof Token ) { return ((Token)payload).getText(); } return t.getPayload().toString(); } private static ST process(String[] ruleNames, Tree t) { if(t.getChildCount()==0) { return literal(getNodeText(t, ruleNames)); } else if(t.getChildCount()==1) { //pass-through rules return process(ruleNames,t.getChild(0)); } else { ST out=template(); out.add("rule_text", getNodeText(t, ruleNames)); for(int i=0;i<t.getChildCount();i++) { out.add("child", process(ruleNames,t.getChild(i))); } return out; } } }
For Kotlin, you can use this extension function fun Tree.format(parser: Parser, indent: Int = 0): String = buildString { val tree = this#format val prefix = " ".repeat(indent) append(prefix) append(Trees.getNodeText(tree, parser)) if (tree.childCount != 0) { append(" (\n") for (i in 0 until tree.childCount) { append(tree.getChild(i).format(parser, indent + 1)) append("\n") } append(prefix).append(")") } }
Are there equivalents to Python's eval() and exec() in Java? [duplicate]
I have a string like the following: String str = "4*5"; Now I have to get the result of 20 by using the string. I know in some other languages the eval() function will do this. How can I do this in Java?
You can use the ScriptEngine class and evaluate it as a Javascript string. ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); Object result = engine.eval("4*5"); There may be a better way, but this one works.
There is no standard Java class or method that will do what you want. Your options include: Select and use some third-party expression evaluation library. For example JEL or any of the half dozen libraries listed here. Wrap the expression in the Java source code for a class with an eval method, send that to the Java compiler, and then load the resulting compiled class. Use some scripting language that can be called from Java as an expression evaluator. Possibilities include Javascript1, BeanShell, and so on. A JSR 223 compliant scripting language implementation can be called via the Scripting API. Write your own expression evaluator from scratch. The first approach is probably simplest. The second and third approaches are a potential security risk if you get the expression to be evaluated from an untrusted user. (Think code injection.) 1 - Javascript in Java SE is a moving target. From Java 6, a version of Mozilla's Rhino Javascript implementation was bundled with Java SE. The in Java 8, it was superseded by Nashorn. In Java 11, Nashorn was deprecated, and finally dropped from the core codebase. As of 2021, both Rhino and Nashorn are being maintained as separate (non-Oracle) products, and Oracle's GraalVM has its own Javascript implementation.
There are very few real use cases in which being able to evaluate a String as a fragment of Java code is necessary or desirable. That is, asking how to do this is really an XY problem: you actually have a different problem, which can be solved a different way. First ask yourself, where did this String that you wish to evaluate come from? Did another part of your program generate it, or was it input provided by the user? Another part of my program generated it: so, you want one part of your program to decide the kind of operation to perform, but not perform the operation, and a second part that performs the chosen operation. Instead of generating and then evaluating a String, use the Strategy, Command or Builder design pattern, as appropriate for your particular case. It is user input: the user could input anything, including commands that, when executed, could cause your program to misbehave, crash, expose information that should be secret, damage persistent information (such as the content of a database), and other such nastiness. The only way to prevent that would be to parse the String yourself, check it was not malicious, and then evaluate it. But parsing it yourself is much of the work that the requested evalfunction would do, so you have saved yourself nothing. Worse still, checking that arbitrary Java was not malicious is impossible, because checking that is the halting problem. It is user input, but the syntax and semantics of permitted text to evaluate is greatly restricted: No general purpose facility can easily implement a general purpose parser and evaluator for whatever restricted syntax and semantics you have chosen. What you need to do is implement a parser and evaluator for your chosen syntax and semantics. If the task is simple, you could write a simple recursive-descent or finite-state-machine parser by hand. If the task is difficult, you could use a compiler-compiler (such as ANTLR) to do some of the work for you. I just want to implement a desktop calculator!: A homework assignment, eh? If you could implement the evaluation of the input expression using a provided eval function, it would not be much of a homework assignment, would it? Your program would be three lines long. Your instructor probably expects you to write the code for a simple arithmetic parser/evaluator. There is well known algorithm, shunting-yard, which you might find useful.
With Java 9, we get access to jshell, so one can write something like this: import jdk.jshell.JShell; import java.lang.StringBuilder; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Eval { public static void main(String[] args) throws IOException { try(JShell js = JShell.create(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { js.onSnippetEvent(snip -> { if (snip.status() == jdk.jshell.Snippet.Status.VALID) { System.out.println("➜ " + snip.value()); } }); System.out.print("> "); for (String line = br.readLine(); line != null; line = br.readLine()) { js.eval(js.sourceCodeAnalysis().analyzeCompletion(line).source()); System.out.print("> "); } } } } Sample run: > 1 + 2 / 4 * 3 ➜ 1 > 32 * 121 ➜ 3872 > 4 * 5 ➜ 20 > 121 * 51 ➜ 6171 > Slightly op, but that's what Java currently has to offer
I could advise you to use Exp4j. It is easy to understand as you can see from the following example code: Expression e = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)") .variables("x", "y") .build() .setVariable("x", 2.3) .setVariable("y", 3.14); double result = e.evaluate();
No, you can not have a generic "eval" in Java (or any compiled language). Unless you're willing to write a Java compiler AND a JVM to be executed inside of your Java program. Yes, you can have some library to evaluate numeric algebraic expressions like the one above - see this thread for discussion.
As previous answers, there is no standard API in Java for this. You can add groovy jar files to your path and groovy.util.Eval.me("4*5") gets your job done.
A fun way to solve your problem could be coding an eval() function on your own! I've done it for you! You can use FunctionSolver library simply by typing FunctionSolver.solveByX(function,value) inside your code. The function attribute is a String which represents the function you want to solve, the value attribute is the value of the independent variable of your function (which MUST be x). If you want to solve a function which contains more than one independent variable, you can use FunctionSolver.solve(function,values) where the values attribute is an HashMap(String,Double) which contains all your independent attributes (as Strings) and their respective values (as Doubles). Another piece of information: I've coded a simple version of FunctionSolver, so its supports only Math methods which return a double value and which accepts one or two double values as fields (just use FunctionSolver.usableMathMethods() if you're curious) (These methods are: bs, sin, cos, tan, atan2, sqrt, log, log10, pow, exp, min, max, copySign, signum, IEEEremainder, acos, asin, atan, cbrt, ceil, cosh, expm1, floor, hypot, log1p, nextAfter, nextDown, nextUp, random, rint, sinh, tanh, toDegrees, toRadians, ulp). Also, that library supports the following operators: * / + - ^ (even if java normally does not support the ^ operator). One last thing: while creating this library I had to use reflections to call Math methods. I think it's really cool, just have a look at this if you are interested in! That's all, here it is the code (and the library): package core; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public abstract class FunctionSolver { public static double solveNumericExpression (String expression) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { return solve(expression, new HashMap<>()); } public static double solveByX (String function, double value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { HashMap<String, Double> values = new HashMap<>(); values.put("x", value); return solveComplexFunction(function, function, values); } public static double solve (String function, HashMap<String,Double> values) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { return solveComplexFunction(function, function, values); } private static double solveComplexFunction (String function, String motherFunction, HashMap<String, Double> values) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int position = 0; while(position < function.length()) { if (alphabetic.contains(""+function.charAt(position))) { if (position == 0 || !alphabetic.contains(""+function.charAt(position-1))) { int endIndex = -1; for (int j = position ; j < function.length()-1 ; j++) { if (alphabetic.contains(""+function.charAt(j)) && !alphabetic.contains(""+function.charAt(j+1))) { endIndex = j; break; } } if (endIndex == -1 & alphabetic.contains(""+function.charAt(function.length()-1))) { endIndex = function.length()-1; } if (endIndex != -1) { String alphabeticElement = function.substring(position, endIndex+1); if (Arrays.asList(usableMathMethods()).contains(alphabeticElement)) { //Start analyzing a Math function int closeParenthesisIndex = -1; int openedParenthesisquantity = 0; int commaIndex = -1; for (int j = endIndex+1 ; j < function.length() ; j++) { if (function.substring(j,j+1).equals("(")) { openedParenthesisquantity++; }else if (function.substring(j,j+1).equals(")")) { openedParenthesisquantity--; if (openedParenthesisquantity == 0) { closeParenthesisIndex = j; break; } }else if (function.substring(j,j+1).equals(",") & openedParenthesisquantity == 0) { if (commaIndex == -1) { commaIndex = j; }else{ throw new IllegalArgumentException("The argument of math function (which is "+alphabeticElement+") has too many commas"); } } } if (closeParenthesisIndex == -1) { throw new IllegalArgumentException("The argument of a Math function (which is "+alphabeticElement+") hasn't got the closing bracket )"); } String functionArgument = function.substring(endIndex+2,closeParenthesisIndex); if (commaIndex != -1) { double firstParameter = solveComplexFunction(functionArgument.substring(0,commaIndex),motherFunction,values); double secondParameter = solveComplexFunction(functionArgument.substring(commaIndex+1),motherFunction,values); Method mathMethod = Math.class.getDeclaredMethod(alphabeticElement, new Class<?>[] {double.class, double.class}); mathMethod.setAccessible(true); String newKey = getNewKey(values); values.put(newKey, (Double) mathMethod.invoke(null, firstParameter, secondParameter)); function = function.substring(0, position)+newKey +((closeParenthesisIndex == function.length()-1)?(""):(function.substring(closeParenthesisIndex+1))); }else { double firstParameter = solveComplexFunction(functionArgument, motherFunction, values); Method mathMethod = Math.class.getDeclaredMethod(alphabeticElement, new Class<?>[] {double.class}); mathMethod.setAccessible(true); String newKey = getNewKey(values); values.put(newKey, (Double) mathMethod.invoke(null, firstParameter)); function = function.substring(0, position)+newKey +((closeParenthesisIndex == function.length()-1)?(""):(function.substring(closeParenthesisIndex+1))); } }else if (!values.containsKey(alphabeticElement)) { throw new IllegalArgumentException("Found a group of letters ("+alphabeticElement+") which is neither a variable nor a Math function: "); } } } } position++; } return solveBracketsFunction(function,motherFunction,values); } private static double solveBracketsFunction (String function,String motherFunction,HashMap<String, Double> values) throws IllegalArgumentException{ function = function.replace(" ", ""); String openingBrackets = "([{"; String closingBrackets = ")]}"; int parenthesisIndex = 0; do { int position = 0; int openParenthesisBlockIndex = -1; String currentOpeningBracket = openingBrackets.charAt(parenthesisIndex)+""; String currentClosingBracket = closingBrackets.charAt(parenthesisIndex)+""; if (contOccouranceIn(currentOpeningBracket,function) != contOccouranceIn(currentClosingBracket,function)) { throw new IllegalArgumentException("Error: brackets are misused in the function "+function); } while (position < function.length()) { if (function.substring(position,position+1).equals(currentOpeningBracket)) { if (position != 0 && !operators.contains(function.substring(position-1,position))) { throw new IllegalArgumentException("Error in function: there must be an operator following a "+currentClosingBracket+" breacket"); } openParenthesisBlockIndex = position; }else if (function.substring(position,position+1).equals(currentClosingBracket)) { if (position != function.length()-1 && !operators.contains(function.substring(position+1,position+2))) { throw new IllegalArgumentException("Error in function: there must be an operator before a "+currentClosingBracket+" breacket"); } String newKey = getNewKey(values); values.put(newKey, solveBracketsFunction(function.substring(openParenthesisBlockIndex+1,position),motherFunction, values)); function = function.substring(0,openParenthesisBlockIndex)+newKey +((position == function.length()-1)?(""):(function.substring(position+1))); position = -1; } position++; } parenthesisIndex++; }while (parenthesisIndex < openingBrackets.length()); return solveBasicFunction(function,motherFunction, values); } private static double solveBasicFunction (String function, String motherFunction, HashMap<String, Double> values) throws IllegalArgumentException{ if (!firstContainsOnlySecond(function, alphanumeric+operators)) { throw new IllegalArgumentException("The function "+function+" is not a basic function"); } if (function.contains("**") | function.contains("//") | function.contains("--") | function.contains("+*") | function.contains("+/") | function.contains("-*") | function.contains("-/")) { /* * ( -+ , +- , *- , *+ , /- , /+ )> Those values are admitted */ throw new IllegalArgumentException("Operators are misused in the function"); } function = function.replace(" ", ""); int position; int operatorIndex = 0; String currentOperator; do { currentOperator = operators.substring(operatorIndex,operatorIndex+1); if (currentOperator.equals("*")) { currentOperator+="/"; operatorIndex++; }else if (currentOperator.equals("+")) { currentOperator+="-"; operatorIndex++; } operatorIndex++; position = 0; while (position < function.length()) { if ((position == 0 && !(""+function.charAt(position)).equals("-") && !(""+function.charAt(position)).equals("+") && operators.contains(""+function.charAt(position))) || (position == function.length()-1 && operators.contains(""+function.charAt(position)))){ throw new IllegalArgumentException("Operators are misused in the function"); } if (currentOperator.contains(function.substring(position, position+1)) & position != 0) { int firstTermBeginIndex = position; while (firstTermBeginIndex > 0) { if ((alphanumeric.contains(""+function.charAt(firstTermBeginIndex))) & (operators.contains(""+function.charAt(firstTermBeginIndex-1)))){ break; } firstTermBeginIndex--; } if (firstTermBeginIndex != 0 && (function.charAt(firstTermBeginIndex-1) == '-' | function.charAt(firstTermBeginIndex-1) == '+')) { if (firstTermBeginIndex == 1) { firstTermBeginIndex--; }else if (operators.contains(""+(function.charAt(firstTermBeginIndex-2)))){ firstTermBeginIndex--; } } String firstTerm = function.substring(firstTermBeginIndex,position); int secondTermLastIndex = position; while (secondTermLastIndex < function.length()-1) { if ((alphanumeric.contains(""+function.charAt(secondTermLastIndex))) & (operators.contains(""+function.charAt(secondTermLastIndex+1)))) { break; } secondTermLastIndex++; } String secondTerm = function.substring(position+1,secondTermLastIndex+1); double result; switch (function.substring(position,position+1)) { case "*": result = solveSingleValue(firstTerm,values)*solveSingleValue(secondTerm,values); break; case "/": result = solveSingleValue(firstTerm,values)/solveSingleValue(secondTerm,values); break; case "+": result = solveSingleValue(firstTerm,values)+solveSingleValue(secondTerm,values); break; case "-": result = solveSingleValue(firstTerm,values)-solveSingleValue(secondTerm,values); break; case "^": result = Math.pow(solveSingleValue(firstTerm,values),solveSingleValue(secondTerm,values)); break; default: throw new IllegalArgumentException("Unknown operator: "+currentOperator); } String newAttribute = getNewKey(values); values.put(newAttribute, result); function = function.substring(0,firstTermBeginIndex)+newAttribute+function.substring(secondTermLastIndex+1,function.length()); deleteValueIfPossible(firstTerm, values, motherFunction); deleteValueIfPossible(secondTerm, values, motherFunction); position = -1; } position++; } }while (operatorIndex < operators.length()); return solveSingleValue(function, values); } private static double solveSingleValue (String singleValue, HashMap<String, Double> values) throws IllegalArgumentException{ if (isDouble(singleValue)) { return Double.parseDouble(singleValue); }else if (firstContainsOnlySecond(singleValue, alphabetic)){ return getValueFromVariable(singleValue, values); }else if (firstContainsOnlySecond(singleValue, alphanumeric+"-+")) { String[] composition = splitByLettersAndNumbers(singleValue); if (composition.length != 2) { throw new IllegalArgumentException("Wrong expression: "+singleValue); }else { if (composition[0].equals("-")) { composition[0] = "-1"; }else if (composition[1].equals("-")) { composition[1] = "-1"; }else if (composition[0].equals("+")) { composition[0] = "+1"; }else if (composition[1].equals("+")) { composition[1] = "+1"; } if (isDouble(composition[0])) { return Double.parseDouble(composition[0])*getValueFromVariable(composition[1], values); }else if (isDouble(composition[1])){ return Double.parseDouble(composition[1])*getValueFromVariable(composition[0], values); }else { throw new IllegalArgumentException("Wrong expression: "+singleValue); } } }else { throw new IllegalArgumentException("Wrong expression: "+singleValue); } } private static double getValueFromVariable (String variable, HashMap<String, Double> values) throws IllegalArgumentException{ Double val = values.get(variable); if (val == null) { throw new IllegalArgumentException("Unknown variable: "+variable); }else { return val; } } /* * FunctionSolver help tools: * */ private static final String alphabetic = "abcdefghilmnopqrstuvzwykxy"; private static final String numeric = "0123456789."; private static final String alphanumeric = alphabetic+numeric; private static final String operators = "^*/+-"; //--> Operators order in important! private static boolean firstContainsOnlySecond(String firstString, String secondString) { for (int j = 0 ; j < firstString.length() ; j++) { if (!secondString.contains(firstString.substring(j, j+1))) { return false; } } return true; } private static String getNewKey (HashMap<String, Double> hashMap) { String alpha = "abcdefghilmnopqrstuvzyjkx"; for (int j = 0 ; j < alpha.length() ; j++) { String k = alpha.substring(j,j+1); if (!hashMap.containsKey(k) & !Arrays.asList(usableMathMethods()).contains(k)) { return k; } } for (int j = 0 ; j < alpha.length() ; j++) { for (int i = 0 ; i < alpha.length() ; i++) { String k = alpha.substring(j,j+1)+alpha.substring(i,i+1); if (!hashMap.containsKey(k) & !Arrays.asList(usableMathMethods()).contains(k)) { return k; } } } throw new NullPointerException(); } public static String[] usableMathMethods () { /* * Only methods that: * return a double type * present one or two parameters (which are double type) */ Method[] mathMethods = Math.class.getDeclaredMethods(); ArrayList<String> usableMethodsNames = new ArrayList<>(); for (Method method : mathMethods) { boolean usable = true; int argumentsCounter = 0; Class<?>[] methodParametersTypes = method.getParameterTypes(); for (Class<?> parameter : methodParametersTypes) { if (!parameter.getSimpleName().equalsIgnoreCase("double")) { usable = false; break; }else { argumentsCounter++; } } if (!method.getReturnType().getSimpleName().toLowerCase().equals("double")) { usable = false; } if (usable & argumentsCounter<=2) { usableMethodsNames.add(method.getName()); } } return usableMethodsNames.toArray(new String[usableMethodsNames.size()]); } private static boolean isDouble (String number) { try { Double.parseDouble(number); return true; }catch (Exception ex) { return false; } } private static String[] splitByLettersAndNumbers (String val) { if (!firstContainsOnlySecond(val, alphanumeric+"+-")) { throw new IllegalArgumentException("Wrong passed value: <<"+val+">>"); } ArrayList<String> response = new ArrayList<>(); String searchingFor; int lastIndex = 0; if (firstContainsOnlySecond(""+val.charAt(0), numeric+"+-")) { searchingFor = alphabetic; }else { searchingFor = numeric+"+-"; } for (int j = 0 ; j < val.length() ; j++) { if (searchingFor.contains(val.charAt(j)+"")) { response.add(val.substring(lastIndex, j)); lastIndex = j; if (searchingFor.equals(numeric+"+-")) { searchingFor = alphabetic; }else { searchingFor = numeric+"+-"; } } } response.add(val.substring(lastIndex,val.length())); return response.toArray(new String[response.size()]); } private static void deleteValueIfPossible (String val, HashMap<String, Double> values, String function) { if (values.get(val) != null & function != null) { if (!function.contains(val)) { values.remove(val); } } } private static int contOccouranceIn (String howManyOfThatString, String inThatString) { return inThatString.length() - inThatString.replace(howManyOfThatString, "").length(); } }
Writing your own library is not that hard as u might thing. Here is link for Shunting-yard algorithm with step by step algorithm explenation. Although, you will have to parse the input for tokens first. There are 2 other questions wich can give you some information too: Turn a String into a Math Expression? What's a good library for parsing mathematical expressions in java?
As there are many answers, I'm adding my implementation on top of eval() method with some additional features like support for factorial, evaluating complex expressions etc. package evaluation; import java.math.BigInteger; import java.util.EmptyStackException; import java.util.Scanner; import java.util.Stack; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class EvalPlus { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { System.out.println("This Evaluation is based on BODMAS rule\n"); evaluate(); } private static void evaluate() { StringBuilder finalStr = new StringBuilder(); System.out.println("Enter an expression to evaluate:"); String expr = scanner.nextLine(); if(isProperExpression(expr)) { expr = replaceBefore(expr); char[] temp = expr.toCharArray(); String operators = "(+-*/%)"; for(int i = 0; i < temp.length; i++) { if((i == 0 && temp[i] != '*') || (i == temp.length-1 && temp[i] != '*' && temp[i] != '!')) { finalStr.append(temp[i]); } else if((i > 0 && i < temp.length -1) || (i==temp.length-1 && temp[i] == '!')) { if(temp[i] == '!') { StringBuilder str = new StringBuilder(); for(int k = i-1; k >= 0; k--) { if(Character.isDigit(temp[k])) { str.insert(0, temp[k] ); } else { break; } } Long prev = Long.valueOf(str.toString()); BigInteger val = new BigInteger("1"); for(Long j = prev; j > 1; j--) { val = val.multiply(BigInteger.valueOf(j)); } finalStr.setLength(finalStr.length() - str.length()); finalStr.append("(" + val + ")"); if(temp.length > i+1) { char next = temp[i+1]; if(operators.indexOf(next) == -1) { finalStr.append("*"); } } } else { finalStr.append(temp[i]); } } } expr = finalStr.toString(); if(expr != null && !expr.isEmpty()) { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); try { System.out.println("Result: " + engine.eval(expr)); evaluate(); } catch (ScriptException e) { System.out.println(e.getMessage()); } } else { System.out.println("Please give an expression"); evaluate(); } } else { System.out.println("Not a valid expression"); evaluate(); } } private static String replaceBefore(String expr) { expr = expr.replace("(", "*("); expr = expr.replace("+*", "+").replace("-*", "-").replace("**", "*").replace("/*", "/").replace("%*", "%"); return expr; } private static boolean isProperExpression(String expr) { expr = expr.replaceAll("[^()]", ""); char[] arr = expr.toCharArray(); Stack<Character> stack = new Stack<Character>(); int i =0; while(i < arr.length) { try { if(arr[i] == '(') { stack.push(arr[i]); } else { stack.pop(); } } catch (EmptyStackException e) { stack.push(arr[i]); } i++; } return stack.isEmpty(); } } Please find the updated gist anytime here. Also comment if any issues are there. Thanks.
There are some perfectly capable answers here. However for non-trivial script it may be desirable to retain the code in a cache, or for debugging purposes, or even to have dynamically self-updating code. To that end, sometimes it's simpler or more robust to interact with Java via command line. Create a temporary directory, output your script and any assets, create the jar. Finally import your new code. It's a bit beyond the scope of normal eval() use in most languages, though you could certainly implement eval by returning the result from some function in your jar. Still, thought I'd mention this method as it does fully encapsulate everything Java can do without 3rd party tools, in case of desperation. This method allows me to turn HTML templates into objects and save them, avoiding the need to parse a template at runtime.
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; class Calculate { public static void main(String[] args) { String strng = "8*-2*3*-1*10/2+6-2"; String[] oparator = {"+","-","*","/"}; List<String> op1 = new ArrayList<>(); String[] x = strng.split(""); int sayac=0; for (String i : x) { sayac ++; for (String c : oparator) { if (i.equals(c)) { try { int j = Integer.parseInt(strng.substring(0, sayac - 1)); op1.add(strng.substring(0, sayac - 1)); op1.add(c); strng = strng.substring(sayac); sayac = 0; }catch (Exception e) { continue; } } } } op1.add(strng); ListIterator<String> it = op1.listIterator(); List<List> newlist = new ArrayList<>() ; while (it.hasNext()) { List<String> p= new ArrayList<>(); p.add(String.valueOf(it.nextIndex())); p.add(it.next()); newlist.add(p); } int sayac2=0; String oparatorvalue = "*"; calculate(sayac2,newlist,oparatorvalue); String oparatorvalue2 = "/"; calculate(sayac2,newlist,oparatorvalue2); String oparatorvalue3 = "+"; calculate(sayac2,newlist,oparatorvalue3); String oparatorvalue4 = "-"; calculate(sayac2,newlist,oparatorvalue4); System.out.println("Result:"+newlist.get(0).get(1)); } private static void calculate(int sayac2, List<List> newlist, String oparatorvalue) { while (sayac2<4){ try{ for (List j : newlist) { if (j.get(1) == oparatorvalue) { Integer opindex = newlist.indexOf(j); Object sayi1 = newlist.get(opindex - 1).get(1); Object sayi2 = newlist.get(opindex + 1).get(1); int sonuc=0; if (oparatorvalue.equals("*")){ sonuc = Integer.parseInt(sayi1.toString()) * Integer.parseInt(sayi2.toString()); } if (oparatorvalue.equals("/")){ sonuc = Integer.parseInt(sayi1.toString()) / Integer.parseInt(sayi2.toString()); } if (oparatorvalue.equals("+")){ sonuc = Integer.parseInt(sayi1.toString()) + Integer.parseInt(sayi2.toString()); } if (oparatorvalue.equals("-")){ sonuc = Integer.parseInt(sayi1.toString()) - Integer.parseInt(sayi2.toString()); } newlist.remove(opindex - 1); newlist.remove(opindex - 1); newlist.remove(opindex - 1); List<String> sonuclist = new ArrayList<>(); sonuclist.add(String.valueOf(opindex - 1)); sonuclist.add(String.valueOf(sonuc)); newlist.add(opindex - 1, sonuclist); }}} catch (Exception e){ continue; } sayac2++;} } }
If you do not want to import heavy scripting library, you can use SimpleExpressionEvaluator directly into your code Usage: Expression.eval("1+2").asString(); // returns "3.0" Expression.eval("1+2").asInt(); // returns 3 Expression.eval("2>3").asString(); // returns "false" Expression.eval("2>3").asBoolean(); // returns false Expression.eval("(3>2)||((2<4)&&(2>1))").asString(); // returns "true" With variables: HashMap<String, Object> st = new HashMap<String, Object>(); st.put("a",1); st.put("b",2); st.put("c",3); st.put("d",4); Expression.eval("a+b", st).asInt(); // or simply asString() Expression.eval("a>b",st).asBoolean(); // or simply asString() Expression.eval("(c>b)||((b<d)&&(b>a))",st).asBoolean(); // or simply asString() Expression.eval("(c>2)||((2<d)&&(b>1))",st).asBoolean(); // or simply asString() Using ExpressionBuilder: Expression.expressionBuilder().putSymbol("a",2).putSymbol("b",3).build("(b>a)").evaluate()
The following resolved the issue: ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); String str = "4*5"; System.out.println(engine.eval(str));
calling a method(constructor) from main & file format
I have a constructor ID3 and I need to start by executing it from the main. Is it possible? I tried doing this: public class ID3 { public static void main(String[] args) throws Exception { System.out.print("\f"); //clears the screen ID3 instance = new ID3("data.txt", 5 , 14 , "", 5); instance.ID3("data.txt", 3 , 5 , " ", 2); //error given here since this line had to be removed } public ID3(String fName, int numAttributes, int testCases, String delimiter, int limitSplits) throws IOException, FileNotFoundException { fileName = fName; n = numAttributes; t = testCases; numSplits = limitSplits; FileInputStream fstream = new FileInputStream(fileName); DataInputStream in = new DataInputStream(fstream); //Parse the first line to see if continuous or discrete attributes. firstLine = new String[n]; firstLine = in.readLine().split(delimiter); int i, j, lineCount = 0; for(i=0; i<n; i++) unusedAttr.add(new Integer(i)); input = new String[t][n+1]; String line; int invalidLines = 0; while((lineCount + invalidLines)<t) { try { input[lineCount] = (in.readLine()).split(delimiter); } catch(NullPointerException e) { invalidLines++;continue; } if (Array.getLength(input[lineCount]) != n+1 || (Array.get(input[lineCount],n).toString().compareTo("?") == 0)) //number of attributes provided in the line is incorrect. { invalidLines++;continue; } lineCount++; } if(invalidLines == t) { System.out.println("All lines invalid - Check the supplied attribute number"); System.exit(0); } if (invalidLines > 0) System.out.println("Not Considering "+invalidLines+" invalid training cases"); if(numSplits > maxSplits || numSplits > (t/2)) { System.out.println("numSplits should be less than or equal to "+Math.min(t/2,limitSplits)); System.exit(1); } t = testCases - invalidLines; thresholdVal = new String[n][numSplits - 1]; boolean allCont = false; if(Array.getLength(firstLine) == 1) { if(firstLine[0].compareTo("c") == 0) allCont = true; else if(firstLine[0].compareTo("d") == 0) return; else { System.out.println("Invalid first line - it should be c or d"); System.exit(1); } } for(i=0; i<n; i++) { if(allCont || firstLine[i].compareTo("c") == 0) //Continuous Attribute { for(j=0; j<numSplits-1; j++) thresholdVal[i][j] = calculateThreshold(i,j); } else if(firstLine[i].compareTo("d") != 0) { System.out.println("Invalid first line - Training data (it should specify if the attributes are c or d)"); System.exit(1); } } for(i=0; i<t; i++) { for(j=0; j<n; j++) { if(allCont || firstLine[j].compareTo("c") == 0) input[i][j] = makeContinuous(input[i][j], j); } } } The code for the constructor is shown above, however it finds the file but doesn't process the data and prints the errors out. How should the file be exactly? Used text file has: d Span Shape Slab long square waffle long rectangle waffle short square two-way short rectangle one-way
You are already calling the constructor here - ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);. You can't call it as a regular method. Just remove the instance.ID3("data.txt", 5 , 14 , "", 5); line.
You cannot call constructors like regular methods. The constructor is automatically called when you create an instance of a class,i.e,when you do ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);
Contructors are not methods. One of the key feature of a method is that it should have a return type (event if it is 'void'). Here, you do not need to explicitly call the constructor again. The functionality you implement in the constructor will be executed at instantiation itself. However, this is not recommended and is bug-prone. You should only be instantiating any variables. The actual functionality should be defined in another method.
Sort algorithm problems on java comparable
I want to do a specific sort. I am using java's comparable interface which means the return of my compare method must return -1 +1 or 0 depending on the equality of the two compared, then I am sorting using Collections. My trouble comes from how I wish to compare. I have a key that is made up of either of the following [keyName] [siteName].[keyName] [siteName].[pageName].[keyName] so as an example "mysite.alampshade.color" the tricky part is the sites must be sorted first, followed by keyname, followed by pageName. but firstly by the keynames, then site name, in the order of the number of sections to the property. Sorry. its a little complicated, an example may help. here is the order they must be: alpha beta charlie sitea.alpha sitea.charlie sitea.pagea.beta sitea.pageb.beta sitea.pagea.charlie siteb.alpha siteb.delta siteb.pagef.alpha siteb.pageb.echo siteb.pageb.golf siteb.pagea.hotel siteb.pageb.hotel siteb.pagec.hotel I have tried many different ways and have thrown away code a few times but still cant get it perfect. some pseudocode would be of great help if not some java. EDIT: to add another possibly simplier to understand example the following is sorted how I need it a b c z a.b a.c a.d a.z a.b.a a.c.a a.b.b a.b.c a.c.c a.a.d b.a b.b b.z b.a.a b.b.a b.a.b c.c.f
Another option, making it recursive you avoid the problem if there is ever more entries. import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class SortTest { public static void main(String[] args) { String[] test = new String[]{ "a", "b", "b.a", "b.a.a", "a.a.a", "a.b.a", "a.a", "a.b", "b.a.b", "b.b.a" }; Arrays.sort(test, new Comparator<String>() { int compareComplexList(List<String> a, List<String> b, List<int[]> positions, int order ) { int minimum = a.size() < b.size() ? a.size() - 1 : b.size() - 1; if (a.get(positions.get(minimum)[order]).compareTo(b.get(positions.get(minimum)[order])) != 0) return a.get(positions.get(minimum)[order]).compareTo(b.get(positions.get(minimum)[order])); else if (order < minimum - 1) return compareComplexList(a,b, positions, ++order); else return Double.compare(a.size(),b.size()); } public int compare(String a, String b) { List<String> partsA = Arrays.asList(a.split("\\.")); List<String> partsB = Arrays.asList(b.split("\\.")); List<int[]> orders = new ArrayList<int[]>(); orders.add(new int[] {0}); orders.add(new int[] {0,1}); orders.add(new int[] {0,2,1}); return compareComplexList(partsA, partsB, orders,0); } }); System.out.println("Sorted: "+Arrays.toString(test)); } }
Should be good now. public int compare(String a, String b) { String[] partsA = a.split("\\."); String[] partsB = b.split("\\."); // If first term is different, we exit. if (partsA[0].compareTo(partsB[0]) != 0) return partsA[0].compareTo(partsB[0]); // Else, first term is identical. else { // Same number of parts if (partsA.length == partsB.length) { // 2 parts, we compare the 2nd part. if (partsA.length == 2) { return partsA[1].compareTo(partsB[1]); // 3 parts, we compare the 3rd part first, then the 2nd part } else { if (partsA[2].compareTo(partsB[2]) != 0) return partsA[2].compareTo(partsB[2]); return partsA[1].compareTo(partsB[1]); } // Different number of parts } else { // If A has only 1 part, it's first if (partsA.length == 1) return -1; // If B has only 1 part, it's first if (partsB.length == 1) return 1; // Case 2 vs 3 parts, we compare the 3rd part with the 2nd part of the other. If it's equal, the shorter is first. if (partsA.length == 3) { if (partsA[2].compareTo(partsB[1]) != 0) return partsA[2].compareTo(partsB[1]); else return 1; } else { if (partsA[1].compareTo(partsB[2]) != 0) return partsA[1].compareTo(partsB[2]); else return -1; } } } }
My other answer started getting too gnarly. Here's a better, more natural solution: public class StrangeComparator { private static class Entry implements Comparable<Entry> { // What to split with. static final String dot = Pattern.quote("."); // The parts. final String key; final String page; final String site; public Entry(String s) { String [] parts = s.split(dot); switch (parts.length) { case 1: key = parts[0]; page = ""; site = ""; break; case 2: key = parts[1]; page = ""; site = parts[0]; break; case 3: key = parts[2]; page = parts[1]; site = parts[0]; break; default: throw new IllegalArgumentException("There must be at least one part to an entry."); } } #Override public int compareTo(Entry t) { int diff = site.compareTo(t.site); if ( diff == 0 ) { diff = page.compareTo(t.page); } if ( diff == 0 ) { diff = key.compareTo(t.key); } return diff; } #Override public String toString () { return (site.length() > 0 ? site + "." : "") + (page.length() > 0 ? page + "." : "") + key; } } public void test() { String[] test = new String[]{ "alpha", "beta", "charlie", "zeta", // Added to demonstrate correctness. "sitea.alpha", "sitea.charlie", "sitea.pagea.beta", "sitea.pageb.beta", "sitea.pagea.charlie", "siteb.alpha", "siteb.delta", "siteb.pagef.alpha", "siteb.pageb.echo", "siteb.pageb.golf", "siteb.pagea.hotel", "siteb.pageb.hotel", "siteb.pagec.hotel" }; Arrays.sort(test); System.out.println("Normal sort: " + Separator.separate("\n", "\n", test)); Entry[] entries = new Entry[test.length]; for ( int i = 0; i < test.length; i++ ) { entries[i] = new Entry(test[i]); } Arrays.sort(entries); System.out.println("Special sort: " + Separator.separate("\n", "\n", entries)); } public static void main(String args[]) { new StrangeComparator().test(); } } Output order is: alpha beta charlie zeta sitea.alpha sitea.charlie sitea.pagea.beta sitea.pagea.charlie sitea.pageb.beta siteb.alpha siteb.delta siteb.pagea.hotel siteb.pageb.echo siteb.pageb.golf siteb.pageb.hotel siteb.pagec.hotel siteb.pagef.alpha Which kinda does what you say but doesn't match your example.
Here's an alternative - if a component is found to contain less that 3 parts then parts are added at the start to take up the slack. It then uses a sort order array to define which columns should be compared next: public void test() { String[] test = new String[]{ "alpha", "beta", "charlie", "zeta", // Added to demonstrate correctness. "sitea.alpha", "sitea.charlie", "sitea.pagea.beta", "sitea.pageb.beta", "sitea.pagea.charlie", "siteb.alpha", "siteb.delta", "siteb.pagef.alpha", "siteb.pageb.echo", "siteb.pageb.golf", "siteb.pagea.hotel", "siteb.pageb.hotel", "siteb.pagec.hotel" }; Arrays.sort(test); System.out.println("Normal sort: "+Arrays.toString(test)); Arrays.sort(test, new Comparator<String>() { // How many columns to pad to. final int padTo = 3; // What to pad with. final String padWith = ""; // What order to compare the resultant columns in. final int[] order = {0, 2, 1}; #Override public int compare(String s1, String s2) { String[] s1parts = padArray(s1.split(Pattern.quote(".")), padTo, padWith); String[] s2parts = padArray(s2.split(Pattern.quote(".")), padTo, padWith); int diff = 0; for ( int i = 0; diff == 0 && i < order.length; i++ ) { diff = s1parts[order[i]].compareTo(s2parts[order[i]]); } return diff; } String [] padArray(String[] array, int padTo, String padWith) { String [] padded = new String[padTo]; for ( int i = 0; i < padded.length; i++ ) { padded[padded.length - i - 1] = i < array.length ? array[i]: padWith; } return padded; } }); System.out.println("Special sort: "+Arrays.toString(test)); } prints (more or less): Normal sort: [alpha, beta, charlie, sitea.alpha, sitea.charlie, sitea.pagea.beta, sitea.pagea.charlie, sitea.pageb.beta, siteb.alpha, siteb.delta, siteb.pagea.hotel, siteb.pageb.echo, siteb.pageb.golf, siteb.pageb.hotel, siteb.pagec.hotel, siteb.pagef.alpha, zeta] Special sort: [alpha, beta, charlie, sitea.alpha, sitea.charlie, siteb.alpha, siteb.delta, zeta, siteb.pagef.alpha, sitea.pagea.beta, sitea.pageb.beta, sitea.pagea.charlie, siteb.pageb.echo, siteb.pageb.golf, siteb.pagea.hotel, siteb.pageb.hotel, siteb.pagec.hotel] There does seem to be some ambiguity in your requirements but this code is structured so you can, with trivial tweaks, achieve most interpretations of your comparison quite simply.