Problème avec un substring apparemment out of bounds mais je ne comprends pas pourquoi
Bonjour,
je débute juste en java et j'ai un soucis avec :
Code:
1 2 3 4 5 6 7 8 9 10 11
| public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String mot;
System.out.println("Quel est le mot à tester ?");
scan.nextLine();
mot = scan.nextLine();
scan.close();
mot.length();
int n = mot.length();
String s1 = mot.substring(1, (n - 1));
} |
J'ai l'erreur Exception in thread "main"
Citation:
java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.substring(Unknown Source)
at tp3.Exo3.main(Exo3.java:16)
mais je ne comprends pas car je rentre des chaines de caractères toujours assez longues comme " abcdef"...
J'ai fait un test avec une chaîne de caractère en dur et ça fonctionne bien, quelqu'un saurait d'où vient l'erreur ?
Merci d'avance!!