Getting an error message – "java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"

huangapple go评论80阅读模式
英文:

Getting an error message - "java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"

问题

抱歉,您的请求不太清楚。您想要我翻译哪部分内容?请提供要翻译的具体文本或句子。

英文:

Hello Guys I was making a program of "Generate all parenthesis" and I got the error message as:-"java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"

along with this I am getting error of stack overflow

I am attaching a code with this error please see to it and tell me the modification if needed:-

public class j{
public static void main(String[] args){
	int n = 2;
	int open = n;
	int close = n;
	String op = " ";
	findAns(op, open, close);
}
private static void findAns(String op, int open, int close){
	if (open == 0 && close == 0){
		System.out.println(op);
	}
	
	if (open == close){
		String op1 = op + "(";
		findAns(op1, open - 1, close);
		return;
	}
	if (open != 0){
		String op1 = op + "(";
		open = open - 1;
		findAns(op1, open, close);
	}
	String op1 = op + ")";
	close = close - 1;
	findAns(op1, open, close);
	return;
}	}

答案1

得分: 1

你需要为递归添加一个退出条件。也许是这样?

if (open == 0 && close == 0){
    System.out.println(op);
    return;
}
英文:

You need an exit condition for your recursion. Maybe this?

if (open == 0 && close == 0){
    System.out.println(op);
--> return;
}

huangapple
  • 本文由 发表于 2020年8月13日 22:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定