IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

String index out of range


Sujet :

Langage Java

  1. #1
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut String index out of range
    Hello,

    Tout d'abord bonne année à vous toutes à vous tous.

    J ai un soucis d index of of bounds dans une petite routine qui lis mot a mot le contenu d un string

    voici mes méthodes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    private static String constructFormatedText(String value) {
    		String result = "";
    		int index = 0;
    		for (int i = 0 ; i < value.length();i++) {
    			String word = readWord(value, index);
    			index += word.length();
    			System.out.println(word);
    		}
    		return result;
    	}
     
    	/**
             * Read the next word in the given String.
             * 
             * @param value
             *                      The value to read
             * @param index
             *                      The begin index
             * @return
             */
    	private static String readWord(String value,int index) {
    		String result = "";
    		int indexStartWord = getIndexOfChar(value,index);
    		int indexEndWord = value.indexOf(' ', indexStartWord) + 1;
    		String word = value.substring(indexStartWord,indexEndWord);
    		return result;
    	}
    	/**
             * Give the index of the next char.
             * @param value
             *                      The value
             * @return int 
             *                      The index of the char
             */
    	private static int getIndexOfChar(String value, int index) {
    		int i;
    		for (i = index; i < value.length(); i++) {
    			char c = value.charAt(i);
    			int ascii = (int) c;
    			if (ascii >= 33 && ascii <= 126) {
    				return i;
    			}
    		}
    		return i;
    	}
    Et le message d erreur obtenu :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1475
    at java.lang.String.substring(String.java:1938)
    at com.odcgroup.page.generation.exporter.TransformationUtils.readWord(TransformationUtils.java:103)
    at com.odcgroup.page.generation.exporter.TransformationUtils.constructFormatedXML(TransformationUtils.java:83)
    at com.odcgroup.page.generation.exporter.TransformationUtils.getXml(TransformationUtils.java:63)
    at com.odcgroup.page.ui.editor.XspTextEditor.updateXspFile(XspTextEditor.java:101)
    at com.odcgroup.page.ui.editor.XspTextEditor.activateEditor(XspTextEditor.java:93)
    at com.odcgroup.page.ui.PageDesignerEditor.pageChange(PageDesignerEditor.java:168)
    at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:239)
    quelqu un aurait une piste ?

  2. #2
    Rédacteur
    Avatar de CyberChouan
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    2 752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Janvier 2007
    Messages : 2 752
    Points : 4 314
    Points
    4 314
    Par défaut
    En gros si je comprends bien, tu as 3 méthodes alambiquées dont le but est de découper un texte en "mots", les mots étant séparés par des espaces...

    Et que dirais-tu de:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String[] motsDuTexte = tonTexte.split(" ");

  3. #3
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut
    Sinon pour ton erreur, c'est relativement clair, tu donnes des paramètres incorrects à ton substring(). En gros, le tableau de caractères que représente ta chaine n'a pas de "case" à l'index -1475 ...

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2007
    Messages
    572
    Détails du profil
    Informations personnelles :
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations forums :
    Inscription : Février 2007
    Messages : 572
    Points : 675
    Points
    675
    Par défaut
    Pour le decoupage en mots, tu devrais aussi jeter un coup d'oeil sur java.util.regex.Pattern (et les Predefined character classes \w et \s)

Discussions similaires

  1. Nouvelle connexion : String index out of range
    Par chris0938 dans le forum Sql Developer
    Réponses: 3
    Dernier message: 02/03/2014, 22h12
  2. Réponses: 4
    Dernier message: 14/01/2010, 18h44
  3. substring exception String index out of range: -1)
    Par tagada30 dans le forum Servlets/JSP
    Réponses: 5
    Dernier message: 30/09/2008, 21h39
  4. IndexError: string index out of range
    Par nina08 dans le forum Général Python
    Réponses: 2
    Dernier message: 04/08/2008, 09h27
  5. probleme de relogin: String index out of range: 0
    Par bbany dans le forum Wildfly/JBoss
    Réponses: 3
    Dernier message: 07/09/2007, 17h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo