views:

59

answers:

1

I am getting an exception when I try to do a replaceAll:

Symbols is a valid string.

private String buildQuery(){
        String query = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(**QUERY**)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";

        deserializeQuotes();

        StringBuffer symbols = new StringBuffer();
        for(int i = 0; i < quotes.size();i++){
            if(i == (quotes.size()-1))
                symbols.append("%22" + quotes.get(i).getSymbol() + "%22%"); //end with a quote
            else
                symbols.append("%22" + quotes.get(i).getSymbol() + "%22%2C");
        }

        query.replaceAll("**QUERY**", symbols.toString());

        return query;
    }

I get the following exception:

08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.os.Looper.loop(Looper.java:123)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at java.lang.reflect.Method.invoke(Method.java:521)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at dalvik.system.NativeStart.main(Native Method)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104): Caused by: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX near index 1:
08-31 10:20:58.307: ERROR/AndroidRuntime(2104): **QUERY**
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):  ^
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at com.ibm.icu4jni.regex.NativeRegEx.open(Native Method)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at java.util.regex.Pattern.compileImpl(Pattern.java:383)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at java.util.regex.Pattern.<init>(Pattern.java:341)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at java.util.regex.Pattern.compile(Pattern.java:358)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at java.lang.String.replaceAll(String.java:2004)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at org.stocktwits.activity.Main.buildQuery(Main.java:193)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at org.stocktwits.activity.Main.createDataModel(Main.java:198)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at org.stocktwits.activity.Main.onCreate(Main.java:73)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-31 10:20:58.307: ERROR/AndroidRuntime(2104):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
+2  A: 

you need to escape *, because it is a quantifier So,

query.replaceAll("\*\*QUERY\*\*", symbols.toString());
dave
Thanks. I changed my expression to something like "REPLACEME" instead.
Sheehan Alam