Java.lang.StringIndexOutOfBoundsException
kevyn93
Messages postés
4
Date d'inscription
Statut
Membre
Dernière intervention
-
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
J'ai le code java suivant:
public class Test1 {
static final String lMaj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static final String lMin = "abcdefghijklmnopqrstuvwxyz";
public static void main(java.lang.String[] args) {
System.out.println(MetMaj("chaine avec MAJ et des min"));
}
public static String MetMaj(String s) {
StringBuffer sb = new StringBuffer(s);
for ( int i = 0; i < sb.length(); i++) {
int index = lMin.indexOf(sb.charAt(i));
System.out.println(index+"=======> "+sb.charAt(i));
if (index <=0 ){
if(sb.charAt(i) != ' '){
sb.setCharAt(i,lMaj.charAt(index));
}
else{
sb.setCharAt(i,' ');
}
}
}
return sb.toString();
}
}
qui me donne l'exception : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Quelqu'un peut m'expliquer où je me suis trompé
Merci
public class Test1 {
static final String lMaj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static final String lMin = "abcdefghijklmnopqrstuvwxyz";
public static void main(java.lang.String[] args) {
System.out.println(MetMaj("chaine avec MAJ et des min"));
}
public static String MetMaj(String s) {
StringBuffer sb = new StringBuffer(s);
for ( int i = 0; i < sb.length(); i++) {
int index = lMin.indexOf(sb.charAt(i));
System.out.println(index+"=======> "+sb.charAt(i));
if (index <=0 ){
if(sb.charAt(i) != ' '){
sb.setCharAt(i,lMaj.charAt(index));
}
else{
sb.setCharAt(i,' ');
}
}
}
return sb.toString();
}
}
qui me donne l'exception : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Quelqu'un peut m'expliquer où je me suis trompé
Merci