0

I got a patternsyntax exception when i am trying to do a String.ReplaceAll.

Below is my String

Number of testcases to execute : 39?    Starting execution of test case : testCreateDAta?    The class that has completed its execution is : test.com.mySpace.service.ejb.session.MyTest?     Finished execution of test case : testCreateDAta?    Starting execution of test case : testUpdate?

What i was trying to do:

junitReportString.replaceAll("?", "\n"); 

The above code fetched me the below exception:

java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0 

Then i tried again with the below code:

junitReportString.replaceAll("\\?", "\n"); 

The above code fetched me the same String that i mentioned above.

My Complete Code:

String junitReportString=new String();
myApp= new URL("http://localhost:port/testWeb/TestJunit.jsp");
URLConnection yc = myApp.openConnection();
yc.connect();
junitReportString=yc.getHeaderField("finalJUNITReport").toString();
System.out.println(junitReportString);
junitReportString.replaceAll("\\?", "\n");
System.out.println("Report Details     ==============>"+junitReportString);

What is wrong with my code. Any guidance much appreciated.

6
  • Your code replaceAll("\\?", "\n"); works for me.. Commented Oct 10, 2014 at 6:48
  • I am not able to print the String in the new line. :( Commented Oct 10, 2014 at 6:49
  • could you post your full code? Commented Oct 10, 2014 at 6:50
  • 1
    I think I know what's going on. You need to assign the string back to the variable: junitReportString = junitReportString.replaceAll("\\?", "\n"); Commented Oct 10, 2014 at 6:51
  • @AvinashRaj : i have posted my whole code. Commented Oct 10, 2014 at 6:53

1 Answer 1

3

#replaceAll does not change the actual String it just returns new String which contains the changes.

junitReportString=junitReportString.replaceAll("\\?", "\n"); 
//Now you have changed String
Sign up to request clarification or add additional context in comments.

1 Comment

OMG..So silly of me...Thanks Buddy..On the target you were.. B-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.