0
public static void main(String s[]) {
        System.out.println(new Binary().meth());
            }
 String meth(){
     String source = "{Hi|Hello|Hey}, how are you {today|doing}?";
     List<String> list1=new ArrayList<String>();
     List<String> list2=new ArrayList<String>();
     String[] strg=source.split("{");
     for(String str: strg){
         String[] data=str.split("}");
         list1.add(data[0]);
         list2.add(data[1]);
     }
     Random random = new Random();
     String[] req=new String[list1.size()];
     int i=0;
     for(String str: list1){
         req[i++]= str.split("|")[random.nextInt(str.split("|").length)];
     }
     int j=0;
     String result="";
     for(String str: list2){
         result=result+req[j++]+str;
     }
     return result;
 }
}

On splitting String on the basiss of caharacter { giving me following error-

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
{
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.closure(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at Binary.meth(Binary.java:16)
    at Binary.main(Binary.java:10)
0

1 Answer 1

5

{ is a special charachter in regex, you need to escape it,

Use below:

source.split("\\{");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.