"Il y a peu de choses qui me soient impossibles..."
"Il y a peu de choses qui me soient impossibles..."
S Majumder wrote:Hi Achyut,
I have tried a little different way :
check it out ..
Thanks ,
Satya
Achyut Dwiwedi wrote:On executing the program, StringOutOfBoundsIndex error occurs.
I am a noob and cannot find the mistake. Help me out.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Liutauras Vilda wrote:Hi Achyut,
In my opinion you should start fixing other things first. As it is very tricky to read your code.
1. Class name suppose to start in capital letter "class Replace".
2. Use brackets in for's and if's even if there is only one statement inside.
3. For the string length variable, choose more meaning variable, for ex. stringLength (very easy to get lost with l in between i, j, k).
4. Line 11. S2 = S; Line 14. S2 = ""; It might should be at line 11. S2 = ""; and omit line 14 ?
5. Line 26. S = S2; Don't really get why is this for and what block of code it belongs to?
I might wrong, but after few modifications i don't get StringOutOfBoundsIndex error. And I still think the code is not doing what it has to do, because of chosen solution itself - but it's really close to.
More experienced guys probably will route you more precisely towards solution.
Liutauras Vilda wrote:StringOutOfBounds occurs in different place.
Modify line 18 "for(int j=i+1;j<l;j++)" to "for(int j=i;j<l;j++)"
It will work on your given example.
But still i think it is not what it has to do in general. Is the idea, that in a string would be only by 1 unique character?
Liutauras Vilda wrote:Fred Rosenberger mentioned, need to track the variables, the line before where the exception is being thrown.
In your code, StringIndexOutOfBoundsException is being thrown at Line 19.
So, he suggested already to add extra statement before line 19 to print index variables. In your case will be i and j.
Achyut Dwiwedi wrote:The error occurs when the value of i=1 and j=8 in line 19.
Any suggestions?
Hello Satya, I would like to know about the Set method you used. What it does and how does it work?
Achyut Dwiwedi wrote:The error occurs when the value of i=1 and j=8 in line 19.
Any suggestions?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
| With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |