Recherche motifen java
Utilisateur anonyme
-
Utilisateur anonyme -
Utilisateur anonyme -
Bonjour à tous,
voilà j'essaye de faire un programme en java, qui doit me retourner la position d'un motif à partir d'un texte donnée,
pas de probleme à la compilation mais à l'exécution et je vois pas d'où peut venir le problème, alors si quelqu'un pouvait m'éclairer.Merci.
Le message d'érreur est:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 1
at java.lang.String.charAt(String.java:444)
at Recherche.comparer(Recherche.java:53)
at Recherche.naive(Recherche.java:37)
at Recherche.main(Recherche.java:60)
Press any key to continue...
merci de votre aide
voilà j'essaye de faire un programme en java, qui doit me retourner la position d'un motif à partir d'un texte donnée,
pas de probleme à la compilation mais à l'exécution et je vois pas d'où peut venir le problème, alors si quelqu'un pouvait m'éclairer.Merci.
Le message d'érreur est:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 1
at java.lang.String.charAt(String.java:444)
at Recherche.comparer(Recherche.java:53)
at Recherche.naive(Recherche.java:37)
at Recherche.main(Recherche.java:60)
Press any key to continue...
public class Recherche{ private String texte, motif; public int k, j, l; public boolean trouve, identik; //Constructeur public Recherche(String texte,String motif){ this.texte = texte; this.motif = motif; } //pré et post condition JML /**<pre><jml> *requires (0 <= longMotif(motif) <= longTexte(texte)); *ensures \result == (0 <= k <= (longMotif(motif) - longTexte(texte)) && (motif.charAt(j) = texte.charAt( k + j))); *</jml></pre> */ public /*@pure@*/int longMotif(String motif){ return motif.length(); } public/*@pure@*/ int longTexte(String texte){ return texte.length(); } public int naive(){ k = -1; trouve = false; while (k<= (longTexte(texte) - longMotif(motif)) && (trouve == false)){ k++; trouve = comparer(); } if (trouve == true){ System.out.println("le motif est dans le texte à la position : "+k); }else{ System.out.println("le motif ne se trouve pas dans le texte"); } return k; } public boolean comparer(){ identik = true; j = -1; while ((j<= longMotif(motif)) && (identik == true)){ j++; identik = ( motif.charAt(j) == texte.charAt(k + j)); } return identik; } public static void main (String [] args){ Recherche m; m = new Recherche("aab","b"); System.out.println("jj"+m.naive()); //test } }
merci de votre aide
A voir également:
- Recherche motifen java
- Jeux java itel - Télécharger - Jeux vidéo
- Waptrick java football - Télécharger - Jeux vidéo
- Recherche automatique des chaînes ne fonctionne pas - Guide
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages