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 "java.lang.ArrayIndexOutOfBoundsException: 0"


Sujet :

avec Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2014
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2014
    Messages : 1
    Points : 2
    Points
    2
    Par défaut Erreur "java.lang.ArrayIndexOutOfBoundsException: 0"
    Bonsoir à tous et merci pour votre temps. Lorsque j'exécute ce code en eclipse , ça m'affiche une erreur "java.lang.ArrayIndexOutOfBoundsException: 0" liée au dernière ligne du 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
    import java.util.*;
    import java.io.*;
     
    /** Simulates Manning & Schützes "Crazy Soft Drink Machine" -
        Generates an output sequence of length t */
    public class soda {
      /** initial state probabilities */
      private int pi_min[];
      private int pi_max[];
      /** transition probabilities */
      private int a_min[][];
      private int a_max[][];
     
      /** output probabilities */
      private int b_min[][];
      private int b_max[][];
     
      /** output symbols */
      private static final int cola = 0;
      private static final int ice_t = 1;
      private static final int lem = 2;
     
      /** states */
      private static final int cola_pref = 0;
      private static final int ice_t_pref = 1;
     
      /** initialization of the HMM as in M&S, page 321 */
      private void init() {
        pi_min = new int[2];
        pi_max = new int[2];
     
        pi_min[cola_pref] = 1;
        pi_max[cola_pref] = 1000;
        pi_min[ice_t_pref] = 0;
        pi_max[ice_t_pref] = 0;
     
        a_min = new int[2][2];
        a_max = new int[2][2];
     
        a_min[cola_pref][cola_pref] = 1;
        a_max[cola_pref][cola_pref] = 700;
        a_min[cola_pref][ice_t_pref] = 701;
        a_max[cola_pref][ice_t_pref] = 1000;
        a_min[ice_t_pref][cola_pref] = 1;
        a_max[ice_t_pref][cola_pref] = 500;
        a_min[ice_t_pref][ice_t_pref] = 501;
        a_max[ice_t_pref][ice_t_pref] = 1000;
     
        b_min = new int[2][3];
        b_max = new int[2][3];
     
        b_min[cola_pref][cola] = 1;
        b_max[cola_pref][cola] = 600;
     
        b_min[cola_pref][ice_t] = 601;
        b_max[cola_pref][ice_t] = 700;
     
        b_min[cola_pref][lem] = 701;
        b_max[cola_pref][lem] = 1000;
     
        b_min[ice_t_pref][cola] = 1;
        b_max[ice_t_pref][cola] = 100;
     
        b_min[ice_t_pref][ice_t] = 101;
        b_max[ice_t_pref][ice_t] = 800;
     
        b_min[ice_t_pref][lem] = 801;
        b_max[ice_t_pref][lem] = 1000;
      }
     
      /** generation of output sequence */
      private void gen_output(int t) {
        try {
          PrintWriter pw = new PrintWriter(new FileWriter("crazysoda.seq"));
     
          pw.println(t);
     
          int rnd_number = ((int) (Math.random() * 1000)) + 1;
          int state;
     
          for (state = 0; state < 2; state++) {
    	if ((pi_min[state] <= rnd_number) && (pi_max[state] >= rnd_number))
    	  break;
          }
     
          for (int i = 0; i < t; i++) {
    	rnd_number = ((int) (Math.random() * 1000)) + 1;
    	for (int symb = 0; symb < 3; symb++) {
    	  if ((b_min[state][symb] <= rnd_number) && (b_max[state][symb]) >= rnd_number) {
    	    printSymbol(symb, pw);
    	    break;
    	  }
    	}
     
    	rnd_number = ((int) (Math.random() * 1000)) + 1;
    	for (int newState = 0; newState < 2; newState++) {
    	  if ((a_min[state][newState] <= rnd_number) && (a_max[state][newState] >= rnd_number)) {
    	    state = newState;
    	    break;
    	  }
    	}
          }
          pw.println();
          pw.close();
        }
        catch (IOException e) {
          System.out.println("crazysoda.seq cannot be loaded.");
          System.exit(0);
        }
      }
     
      /** output of a Symbol */
      private void printSymbol(int s, PrintWriter pw) {
        pw.print(s);
      }
     
      /** Main program. Invoke with java soda <t>, where t is the length of
          the sequence to be generated. */
      public static void main(String argv[]) {
        soda s = new soda();
        s.init();
        s.gen_output(Integer.parseInt(argv[0]));
      }
    }

  2. #2
    Membre chevronné

    Profil pro
    Inscrit en
    Décembre 2011
    Messages
    974
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2011
    Messages : 974
    Points : 1 825
    Points
    1 825
    Par défaut
    clic droit sur Soda.java -> run as -> run configurations . Dans l'onglet "arguments", il faut renseigner la valeur de l'argument.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    java
    Inscrit en
    Août 2014
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : java
    Secteur : Conseil

    Informations forums :
    Inscription : Août 2014
    Messages : 20
    Points : 27
    Points
    27
    Par défaut
    Citation Envoyé par Mourad19 Voir le message
    Bonsoir à tous et merci pour votre temps. Lorsque j'exécute ce code en eclipse , ça m'affiche une erreur "java.lang.ArrayIndexOutOfBoundsException: 0" liée au dernière ligne du 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
    import java.util.*;
    import java.io.*;
     
    /** Simulates Manning & Schützes "Crazy Soft Drink Machine" -
        Generates an output sequence of length t */
    public class soda {
      /** initial state probabilities */
      private int pi_min[];
      private int pi_max[];
      /** transition probabilities */
      private int a_min[][];
      private int a_max[][];
     
      /** output probabilities */
      private int b_min[][];
      private int b_max[][];
     
      /** output symbols */
      private static final int cola = 0;
      private static final int ice_t = 1;
      private static final int lem = 2;
     
      /** states */
      private static final int cola_pref = 0;
      private static final int ice_t_pref = 1;
     
      /** initialization of the HMM as in M&S, page 321 */
      private void init() {
        pi_min = new int[2];
        pi_max = new int[2];
     
        pi_min[cola_pref] = 1;
        pi_max[cola_pref] = 1000;
        pi_min[ice_t_pref] = 0;
        pi_max[ice_t_pref] = 0;
     
        a_min = new int[2][2];
        a_max = new int[2][2];
     
        a_min[cola_pref][cola_pref] = 1;
        a_max[cola_pref][cola_pref] = 700;
        a_min[cola_pref][ice_t_pref] = 701;
        a_max[cola_pref][ice_t_pref] = 1000;
        a_min[ice_t_pref][cola_pref] = 1;
        a_max[ice_t_pref][cola_pref] = 500;
        a_min[ice_t_pref][ice_t_pref] = 501;
        a_max[ice_t_pref][ice_t_pref] = 1000;
     
        b_min = new int[2][3];
        b_max = new int[2][3];
     
        b_min[cola_pref][cola] = 1;
        b_max[cola_pref][cola] = 600;
     
        b_min[cola_pref][ice_t] = 601;
        b_max[cola_pref][ice_t] = 700;
     
        b_min[cola_pref][lem] = 701;
        b_max[cola_pref][lem] = 1000;
     
        b_min[ice_t_pref][cola] = 1;
        b_max[ice_t_pref][cola] = 100;
     
        b_min[ice_t_pref][ice_t] = 101;
        b_max[ice_t_pref][ice_t] = 800;
     
        b_min[ice_t_pref][lem] = 801;
        b_max[ice_t_pref][lem] = 1000;
      }
     
      /** generation of output sequence */
      private void gen_output(int t) {
        try {
          PrintWriter pw = new PrintWriter(new FileWriter("crazysoda.seq"));
     
          pw.println(t);
     
          int rnd_number = ((int) (Math.random() * 1000)) + 1;
          int state;
     
          for (state = 0; state < 2; state++) {
    	if ((pi_min[state] <= rnd_number) && (pi_max[state] >= rnd_number))
    	  break;
          }
     
          for (int i = 0; i < t; i++) {
    	rnd_number = ((int) (Math.random() * 1000)) + 1;
    	for (int symb = 0; symb < 3; symb++) {
    	  if ((b_min[state][symb] <= rnd_number) && (b_max[state][symb]) >= rnd_number) {
    	    printSymbol(symb, pw);
    	    break;
    	  }
    	}
     
    	rnd_number = ((int) (Math.random() * 1000)) + 1;
    	for (int newState = 0; newState < 2; newState++) {
    	  if ((a_min[state][newState] <= rnd_number) && (a_max[state][newState] >= rnd_number)) {
    	    state = newState;
    	    break;
    	  }
    	}
          }
          pw.println();
          pw.close();
        }
        catch (IOException e) {
          System.out.println("crazysoda.seq cannot be loaded.");
          System.exit(0);
        }
      }
     
      /** output of a Symbol */
      private void printSymbol(int s, PrintWriter pw) {
        pw.print(s);
      }
     
      /** Main program. Invoke with java soda <t>, where t is the length of
          the sequence to be generated. */
      public static void main(String argv[]) {
        soda s = new soda();
        s.init();
        s.gen_output(Integer.parseInt(argv[0]));
      }
    }
    c'est un Exception
    ArrayIndexOutOfBoundsException :a cause d'utilisation d'un index de tableau erroné

Discussions similaires

  1. erreur tomcat : java.lang.ArrayIndexOutOfBoundsException
    Par marwa zd dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 11/04/2014, 00h49
  2. Réponses: 4
    Dernier message: 18/04/2013, 10h41
  3. Erreur : java.lang.ArrayIndexOutOfBoundsException
    Par the watcher dans le forum Langage
    Réponses: 10
    Dernier message: 22/09/2010, 18h56
  4. Erreur java lang.ArrayIndexOutOfBoundsException
    Par lerorodu51 dans le forum NetBeans
    Réponses: 8
    Dernier message: 04/06/2009, 19h17

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