I've written a function which can get the environment variable's value. The input parameter would one of the following:${machine}/bin${machine/bin}${machine}/bin${path${OS}}/pathI would need to check the values which start with ${} I've written a function which is recursive in nature, and I would appreciate it if anyone can review the code for me and tell me what can be done better:private String replaceWithEnvironmentVariables(String specialChars) {    String result=;    boolean flag=true;    try{        result = specialChars;        String temp = ;        char loopBreaker = '}';        char dollarCheck = '$';        char slashCheck = '/';        int index = result.indexOf({);        if (index == -1){            result = result.replaceAll([{},$], );            return result;        }        for (int i = index; i < result.length(); i++) {            if(slashCheck==result.charAt(i))                break;            temp += result.charAt(i);            if(dollarCheck==result.charAt(i) || loopBreaker==result.charAt(i)){                break;            }        }        String sub = temp.replaceAll([{},$/], );        if(sub==null || sub.length()==0){            sub=;            flag=false;            result = result.replace(temp, sub);        }        if(flag){            String returnSub = getEnvironmentVariableValue(sub);            result = result.replace(temp, returnSub);        }    }catch(NullPointerException nullPointer){        nullPointer.printStackTrace();    }catch(Exception except){        except.printStackTrace();    }    return replaceWithEnvironmentVariables(result);}
Get the environment variable's value
java;recursion
I really have a hard time figuring out what you want to archive. Maybe a working example would help me; especially one containing /.So this is what I would suggest so far/* * These variables always have the same value. Make constants of them. */public static final char LOOP_BREAKER = '}';public static final char DOLLAR_CHECK = '$';public static final char SLASH_CHECK  = '/';private static String replaceWithEnvironmentVariables(String specialChars) {    // This is the only time a NullPointerException can occur inside this function - prevent it!    if(null == specialChars) {        return null;    }    // Why should result be first assigned ? Up to the point where specialChars is assigned to result, no Exception can occur.    String result = specialChars;    // We need to include the $ if you are only interested in things starting with ${    final int index = result.indexOf(${);    if (index == -1){        // No need to update result - just return it        return result.replaceAll([{},$], );    }    // declare outside loop, so we can use it outside loop    int groupEnd;    // start at index + 2 as we know that ${ is at index    for (groupEnd = index + 1; groupEnd < result.length(); groupEnd++) {        // cache char, so we don't have the overhead of a function-call each check        final char c = result.charAt(groupEnd);        if(SLASH_CHECK == c){            // Don't include '/'            break;        }        if(DOLLAR_CHECK == c || LOOP_BREAKER == c) {            // Include '$' or '}' then break            groupEnd++;            break;        }    }    // Creating temp like this will utilize array-copying-mechanics, which should be faster than appending single characters    final String temp = result.substring(index, groupEnd);    // [{},$/] is searching for too many characters as we stop, when we hit '/'    final String sub = temp.replaceAll([{},$], );    // sub can not be null    if(sub.length() == 0){        // no need for flag. It is either this block or the next        // also no need to assign  to sub as it is never again used outside this block        result = result.replace(temp, );    } else {        // As I do not know what getEnvironmentVariableValue does, you may need your catch-clauses here. Also I would assume, that you need a return-statement in both catch-clauses as otherwise you are going to call the function with the same parameter over and over again as result is equal to specialChars at this point        try{            String returnSub = getEnvironmentVariableValue(sub);            // replace will throw a NullPointerException when we try to replace with null            if(null == returnSub) {                result = result.replace(temp, );            } else {                result = result.replace(temp, returnSub);            }        }catch(NullPointerException nullPointer){            nullPointer.printStackTrace();        }catch(Exception except){            except.printStackTrace();        }    }    return replaceWithEnvironmentVariables(result);}