Reputation: 653
I have this small piece of code that aims to replace accented letter with no accented letters.
protected String sinAcentos(String str) {
// Cadena de caracteres original a sustituir.
String original = "áàäéèëíìïóòöúùuñÁÀÄÉÈËÍÌÏÓÒÖÚÙÜÑçÇ";
// Cadena de caracteres ASCII que reemplazarán los originales.
String ascii = "aaaeeeiiiooouuunAAAEEEIIIOOOUUUNcC";
String tmp = str;
for (int i=0; i<original.length(); i++) {
// Reemplazamos los caracteres especiales.
tmp = tmp.replace(original.charAt(i), ascii.charAt(i));
}//for i
return tmp;
}
When I run this function from within IDE (eclipse) there is no error. But then I export an create an executable product, that runs on Windows 7 and also Windows XP.
When the function runs an error occurs and the text is:
Index out of bounds:34
So, 34 is the lenght of original string variable. therefore the loop shoul loop i between 0 and 33 , because the loop condition : i < original.lenght()
...
I change the code adding a try...catch at the replace line, and that's how is working now.
Any idea what's wrong in the code?
Upvotes: 0
Views: 116
Reputation: 13101
Please check the encoding of your Java source file and also check the charset used for both Strings.
There might be a mismatch between those and some encodings require more bytes than others for special characters (think UTF-8 vs. ISO-8859-1 for example).
Make sure that your Java source file is using UTF-8 and not ISO-8859-1 or a Windows-specific encoding like CP-1252.
Upvotes: 3