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

avec Java Discussion :

Erreur ArrayIndexOutOfBoundException incompréhensible ?


Sujet :

avec Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Mars 2008
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 6
    Points : 7
    Points
    7
    Par défaut Erreur ArrayIndexOutOfBoundException incompréhensible ?
    Bonjour,

    Je débute en Java et je rencontre une erreur que je n'arrive pas à corriger.

    Voici le code :
    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    import java.util.regex.Pattern;
     
    /**
     *
     * @author bareri00
     */
    public class brouillon {
     
        // private static char[] cBases = new char[strCIGAR.length()];
        // private static int[] basePOS = new int[strCIGAR.length()];
        private static int startPOS = 7;
     
        // private static int[] nucPOS = {};
        // int[] basePOS = new int[strCIGAR.length()];
        private static final String strCIGAR = "8M2I4M1D3M";
        private static final String strSEQ   = "TTAGATAAAGGATACTG";
     
        // private static char[] charCIGAR = new char[(getCIGARoperations(strCIGAR)).length];
        // private static int[] valCIGAR = new int[(getCIGARvalues(strCIGAR)).length];
        public static void main(String[] args) throws Exception {
            int[] basePOS = new int[strCIGAR.length()];
     
            basePOS = getReadPOS(strCIGAR, strSEQ, startPOS);
            System.out.println(basePOS.length);
     
            /*
             * char[] cg = getCIGARoperations(strCIGAR);
             * //char[] cg = StringToCharArray(strCIGAR);
             * for (char i:cg){
             *  System.out.println(i);
             * }
             *
             *
             * char[] out = new char[strCIGAR.length()];
             * char[] inArray = StringToCharArray(strCIGAR);
             * for(int i=0;i<inArray.length;i++){
             *  if(("M".charAt(0)== inArray[i]) || ("H".charAt(0)== inArray[i]) || ("S".charAt(0)== inArray[i])||("I".charAt(0)== inArray[i]) ||("D".charAt(0)== inArray[i])||("P".charAt(0)== inArray[i])||("N".charAt(0)== inArray[i]) ||("X".charAt(0)== inArray[i])||("=".charAt(0)== inArray[i])){
             *      out[i]=inArray[i];
             *  }
             * }
             * for (char i:out){
             *  System.out.println(i);
             * }
             */
        }
     
        private static char[] StringToCharArray(String aString) {
            char[] aCharArray;
     
            aCharArray = aString.toCharArray();
     
            return aCharArray;
        }
     
        private static int[] getReadPOS(String aCIGAR, String aSEQ, int POS_start) throws Exception {
            int[]  nucPOS      = new int[aSEQ.length()];
            int    outBoundPOS = 100000000;
            char[] aCharCIGAR  = getCIGARoperations(aCIGAR);
            int[]  aValCIGAR   = convertStringArraytoIntArray(getCIGARvalues(aCIGAR));
            int[]  intCIGAR    = new int[aValCIGAR.length];
     
            for (int i = 0; i < aCharCIGAR.length; i++) {
                if ("M".charAt(0) == aCharCIGAR[i]) {
                    intCIGAR[i] = aValCIGAR[i];
     
                    for (int j = 0; j < intCIGAR[i]; j++) {
                        nucPOS[j] = POS_start + j;
                        POS_start = POS_start + intCIGAR[i];
                    }
                }
     
                if (("D".charAt(0) == aCharCIGAR[i]) || ("N".charAt(0) == aCharCIGAR[i])) {
                    intCIGAR[i] = aValCIGAR[i];
                    POS_start   = POS_start + intCIGAR[i];
                }
     
                if (("I".charAt(0) == aCharCIGAR[i]) || ("S".charAt(0) == aCharCIGAR[i])) {
                    POS_start   = POS_start + 1;
                    intCIGAR[i] = aValCIGAR[i];
     
                    for (int j = 0; j < intCIGAR[i]; j++) {
                        nucPOS[j] = outBoundPOS;
                    }
                }
     
                if ("P".charAt(0) == aCharCIGAR[i]) {
                    POS_start = POS_start + 1;
                }
            }
     
            return nucPOS;
        }
     
        private static String[] getCIGARvalues(String CIGARstr) {
            String[] outArray = new String[CIGARstr.length()];
            String   pattern  = "[HMISDPN]";
            Pattern  splitter = Pattern.compile(pattern);
     
            outArray = splitter.split(CIGARstr);
     
            return outArray;
        }
     
        private static char[] getCIGARoperations(String CIGARstr) {
            char[] outArray = new char[CIGARstr.length()];
            char[] inArray  = StringToCharArray(CIGARstr);
     
            for (int i = 0; i < inArray.length; i++) {
                if (("M".charAt(0) == inArray[i]) || ("H".charAt(0) == inArray[i]) || ("S".charAt(0) == inArray[i])
                        || ("I".charAt(0) == inArray[i]) || ("D".charAt(0) == inArray[i]) || ("P".charAt(0) == inArray[i])
                        || ("N".charAt(0) == inArray[i]) || ("X".charAt(0) == inArray[i])
                        || ("=".charAt(0) == inArray[i])) {
                    outArray[i] = inArray[i];
                }
            }
     
            return outArray;
        }
     
        public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
            if (sarray != null) {
                int intarray[] = new int[sarray.length];
     
                for (int i = 0; i < sarray.length; i++) {
                    intarray[i] = Integer.parseInt(sarray[i]);
                }
     
                return intarray;
            }
     
            return null;
        }
    }

    Quand j'excute, j'obtiens l'erreur suivante:
    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at mutationcaller.brouillon.getReadPOS(brouillon.java:64)
    at mutationcaller.brouillon.main(brouillon.java:27)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)
    La ligne qui pose problème est :

    intCIGAR[i] = aValCIGAR[i];
    J'ai beau revoir le script plusieurs fois, je ne vois pas le problème.
    La taille du tableau me semble correcte, mais comme je suis débutant, l'oeil d'un expert m'aiderait.

    Merci.

  2. #2
    Modérateur

    Avatar de Robin56
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juin 2009
    Messages
    5 297
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juin 2009
    Messages : 5 297
    Points : 13 670
    Points
    13 670
    Par défaut
    Dans ces cas là le mode pas à pas est pratique pour voir les valeurs de chaque chose. La solution du pauvre c'est de mettre des traces dans ton code.

    La question que je me poserais c'est : Suis-je certains que aCharCIGAR n'est pas un tableau avec plus d'élément que intCIGAR ou aValCIGAR ?
    Responsable Java de Developpez.com (Twitter et Facebook)
    Besoin d'un article/tutoriel/cours sur Java, consulter la page cours
    N'hésitez pas à consulter la FAQ Java et à poser vos questions sur les forums d'entraide Java
    --------
    Architecte Solution
    LinkedIn : https://www.linkedin.com/in/nicolascaudard/

  3. #3
    Membre émérite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2008
    Messages
    1 190
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2008
    Messages : 1 190
    Points : 2 657
    Points
    2 657
    Par défaut
    La taille de l'un ou l'autre de tes tableaux est inférieur à 5.
    Donc ---> debug (avec un IDE comme eclipse c'est facile) ou trace (System.out.println) comme dis plus haut!

Discussions similaires

  1. erreur 405 incompréhensible
    Par kitch18 dans le forum Langage
    Réponses: 11
    Dernier message: 11/05/2008, 22h25
  2. Erreur Windows : incompréhension !
    Par Matalobos dans le forum GTK+ avec C & C++
    Réponses: 23
    Dernier message: 03/05/2007, 15h12
  3. Erreur javascript incompréhensible
    Par guidav dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 25/01/2007, 10h12
  4. erreur javascript incompréhensible
    Par amelhog dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 18/08/2005, 16h50
  5. erreur 104 incompréhensible
    Par shadowmoon dans le forum Langage SQL
    Réponses: 5
    Dernier message: 18/05/2005, 16h16

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