• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot find error in code  RSS feed

 
Achyut Dwiwedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to create a java program which can remove the repeated characters in a String. For ex-
Input: kamehamehaaa
Output: kameh
Here is my code:-



On executing the program, StringOutOfBoundsIndex error occurs.
I am a noob and cannot find the mistake. Help me out.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. I've reformatted your code with code tags, so it's easier to read and discuss.
 
Achyut Dwiwedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, any solution to my noobish mistakes yet?
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Achyut,
I have tried a little different way :



check it out ..

Thanks ,
Satya
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wish I could help at the moment, Achyut, but suddenly my NetBeans IDE won't start. Maybe others can help while I'm trying to fix that.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Achyut Dwiwedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Majumder wrote:Hi Achyut,
I have tried a little different way :



check it out ..

Thanks ,
Satya


Hello Satya, I would like to know about the Set method you used. What it does and how does it work?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Achyut Dwiwedi wrote:On executing the program, StringOutOfBoundsIndex error occurs.
I am a noob and cannot find the mistake. Help me out.

It should tell you on what line the exception occurs. This is important information!!!

Once you know the line, what you can do is print out the index variable just before that line (i.e. insert a new line of code). Then, look and see WHY the index is larger than the size of your array.
 
Achyut Dwiwedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Well, I really don't know how to edit my code.
In Line 26. S = S2; was written by me to just copy the S2 string to S string, which would facilitate the normal functioning of the for loop block. I hope you would understand what I want to do if you read my code again.
I also tried to edit the code to remove the StringOutOfBoundsIndex error by removing the line 14 but the program didn't fulfil its purpose.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Achyut Dwiwedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


But if there was a problem in those lines the loop won't even execute as OutOfBounds error will occur only when the index number equals or surpasses variable l, i.e. the length of the string.
I hope I'm not explaining my views in a bad way.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


The error occurs when the value of i=1 and j=8 in line 19.
Any suggestions?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Achyut Dwiwedi wrote:The error occurs when the value of i=1 and j=8 in line 19.
Any suggestions?


in addition, track S.length() as well

If you're using your original input "kamehamehaaa", error occurs when when S.length() = 8, i = 2, j = 8.

Try to check yourself, and think, what is wrong with that and how it is happens
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Satya, I would like to know about the Set method you used. What it does and how does it work?


Set is not a method , it's an Interface which contains unique values . you can read webpage

Hope you understand ..

Thanks,
Satya
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Achyut Dwiwedi wrote:The error occurs when the value of i=1 and j=8 in line 19.
Any suggestions?

well, in the code you have posted, line 19 is a closing brace. So we can't really tell you much, because we don't know what your code looks like now.

but an array out of bounds exception means you are trying to access an element beyond the end of the array. look at the size of the array and what element you are trying to access. your logic is wrong somewhere in determining what element you want to look at.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!