+ 1
java.lang.IndexOutOfBoundsException
Code keeps trying to find index 48 out of an array which has less elements. It is not supposed to even seek for the index. Can anyone tell me why this is happening? Thanks! https://code.sololearn.com/c6dGa784RRdp/?ref=app
4 ответов
+ 5
As far as I know remove() is for removing the index, not the element. And it takes integer not char.
For example .remove(7) removes the element at index 7.
In your second loop where you want to remove digits, it converts char '0' to int('0') which is 48 (ascci value).
And your list has no index 48.
Btw: Why do you want to remove the elements? Why not just counting?
+ 5
Btw: It seems to be a Java thingy, that chars gets automatically converted to int instead of throwing an error.
So you should be careful with methods which takes integers.
https://code.sololearn.com/c6T56zkgaBbB/?ref=app
+ 4
The base type for char is int in Java so it will match the signature for the
remove(int index) method instead of the
remove(Object o) method.
If you change char to Character in all of your for loops the code will then run successfully without this error, however you will still have a logic issue when it comes to possible repeating characters within the text. The Object version of the remove method will only remove the first match that it finds.
+ 1
ChaoticDawg Denise Roßberg thank you both for the help! It works now. Have a nice day all :)