Plz Help me fix this runtime error : | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Plz Help me fix this runtime error :

Q. The question is we have to replace the occurrence of the "ab" with "12" and return the new string and recursion is to be used, so if, the input string is "xabx" the output should be "x12x". public class Replaceab { public static String replaceH(String input,int index,String help){ if(index == input.length()){ return help; } if(input.substring(index,index+2).equals("ab")){ help = help + "12"; return replaceH(input,index+2,help); }else{ help = help + input.substring(index,index+1); return replaceH(input,index+1,help); } } // Return the changed string public static String replace(String input){ if(input.length()==1){ return input; } String help = ""; return replaceH(input,0,help); } public static void main(String[] args) { // TODO Auto-generated method stub String s = "xabx"; s = replace(s); System.out.println(s); /* the ideal output should be x12x but I am getting runtime error */ } }

18th Sep 2017, 7:18 AM
Antoreep Jana
Antoreep Jana - avatar
10 Answers
+ 2
I haven't gone through all your code, but this may be the source of the problem; change: if(input.substring(index,index+2)=="ab") to: if(input.substring(index,index+2).equals("ab")) String comparisons using == compare the object not the value.
18th Sep 2017, 7:05 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ok, also you need to change: if(index==input.length()) to: if(index==input.length()-1)
18th Sep 2017, 7:15 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Is this a hacker challenge, like the ones from hacker earth? If so can you post a link or at least the requirements and test cases? Adding this worked for me on this case, but it may not work with other cases: if(index + 1 == input.length()){ return help + input.substring(index); }
18th Sep 2017, 7:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
My other answer should fix the out of bounds exception. but you'll still need to append the last character to the help String.
18th Sep 2017, 7:19 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Now, this change worked for 4 test cases ( earlier one was working for 2 cases) out of 6. Btw what logic did you hit upon for that edit ? Sorry friend, It's not a publically visible question. So link sharing won't work else I would have shared the same in the beginning :).
18th Sep 2017, 7:47 AM
Antoreep Jana
Antoreep Jana - avatar
+ 1
The output was missing the last character prior to that. The substring when given an index will go from that index to the end of the String. If help is missing the last char then the index is at the end of the input string and we need to append it. Do you have to use recursion? can you copy the challenge and post it here? etc etc lol
18th Sep 2017, 7:53 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Did a little testing and it looks like it wouldn't pass if the input was "ab" or a repeating pattern of "ababababab" etc. So I altered the code a little bit. Try this and see if it passes. public class Replaceab { public static String replaceH(String input,int index,String help){ if(index + 1 == input.length()){ return help + input.substring(index); } if(index + 1 >= input.length()-1 && input.substring(index,index+2).equals("ab")) { return help + "12"; } int endIndex = (index + 2) <= (input.length()-1) ? index + 2 : input.length() -1; if(input.substring(index,endIndex).equals("ab")){ help = help + "12"; return replaceH(input,endIndex,help); }else{ help = help + input.substring(index,index+1); return replaceH(input,index+1,help); } } public static String replace(String input){ if(input.length()==1){ return input; } String help = ""; return replaceH(input,0,help); } public static void main(String[] args) { String s = "abababababab"; s = replace(s); System.out.println(s); } }
18th Sep 2017, 9:26 AM
ChaoticDawg
ChaoticDawg - avatar
0
ya sorry, I copied my old snippet. The correct one is with .equals() and I am unable to figure out why it is not clearing all the test cases. Only two of the test cases are going correct and remaining ones are showing Runtime error ( String Index Out of Bounds Exception).
18th Sep 2017, 7:17 AM
Antoreep Jana
Antoreep Jana - avatar
0
ya logically, I also hit upon the same thought of changing (index== input.length()-1) . But after implying the same, none of the test cases pass. :(
18th Sep 2017, 7:20 AM
Antoreep Jana
Antoreep Jana - avatar
0
Give a manual dry run to the code, I think it should run fine. When I tried to figure out what is actually happening, I used the debugger mode of eclipse and found that the if condition " if(input.substring(index,index+2).equals("ab"))" is being continuously being omitted. Plz help me out.
18th Sep 2017, 7:23 AM
Antoreep Jana
Antoreep Jana - avatar