This is my code for prepare a
string as a
java source code. But it is not giving correct output. How can I fix it with this code?
String tab = "";
String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");if(\"A\".equals(\"A\")){return \"A\";}System.out.println(\"ABC\");}}";
String outtext = code;
String repfrom = "{";
String repto = "{\n";
Pattern p = Pattern.compile(repfrom, Pattern.LITERAL);
Matcher m = p.matcher(outtext);
int counter = 0;
StringBuffer sb = new StringBuffer();
while (m.find()) {
counter++;
m.appendReplacement(sb, repto + (tab += "\t"));
}
m.appendTail(sb);
String newtext = sb.toString().replace(";", ";\n");
System.out.println(newtext);
My output is
class Demo{
public static void main(String[] args) {
System.out.println("ABC");
if("A".equals("A")){
return "A";
}System.out.println("ABC");
}}
Thanks in Advance