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 "variable dimension2 might not have been initialized"


Sujet :

avec Java

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2011
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Novembre 2011
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Erreur "variable dimension2 might not have been initialized"
    Bonjour,

    Je suis débutant en Java.

    Voici le problème que je rencontre
    Pox.java:149: variable dimension2 might not have been initialized
    for(int ip = 0 ;ip < dimension2; ip++)
    ^
    1 error
    Dans mon 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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    import java.io.*;
    import java.util.StringTokenizer;
    import java.util.Scanner;
     
    public class Pox
    {
        public static void main(String [] args) throws Exception
        {
     
    //lire la file
            String filename = args[0];
            BufferedReader in = new BufferedReader(new FileReader(filename));
            String line;
            int val= 0;
            int dimension1=0;
            int dimension2=0;
            int temp=0;
            int ip=0;
            String token;
    //line par line
            while ((line = in.readLine()) != null)
            {
                StringTokenizer st = new StringTokenizer(line);
                while (st.hasMoreTokens())
                {
                    token = st.nextToken();
     
    //chercher les commandes
     
                    if (token.equals("MEM"))
                    {
     
                        val = Integer.parseInt(st.nextToken());
                        dimension1=val;
                    }
                    if (token.equals("INST"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        dimension2=val;
                    }
                }
            }
     
            in = new BufferedReader(new FileReader(filename));
            int []t = new int[dimension1+dimension2];
            while ((line = in.readLine()) != null)
            {
                StringTokenizer st = new StringTokenizer(line);
                while (st.hasMoreTokens())
                {
                    token = st.nextToken();
                    if (token.equals("READ"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=101;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("WRITE"))
                        {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=102;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("LOAD"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=103;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("STORE"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=104;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("ACC"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=105;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("INC"))
                    {
                        t[ip]=106;
                    }
                    if (token.equals("DEC"))
                    {
                        t[ip]=107;
                    }
                    if (token.equals("ADDV"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=108;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("ADD"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=109;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("SUB"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]=110;ip++;
                        t[ip]=val;
                    }
                    if (token.equals("MUL"))
                    {
                        val = Integer.parseInt(st.nextToken());
                        t[ip]= 111;ip++;
                        t[ip]= val;
                    }
                    if (token.equals("JUMP"))
                    {
                        t[ip]=112;
                    }
                    if (token.equals("JUMZ"))
                    {
                        t[ip]=113;
                    }
                    if (token.equals("IFNEG"))
                    {
                        t[ip]=114;
                    }
                    if (token.equals("STOP"))
                    {
                        t[ip]=115;
                    }
                    ip++;
                }
            }
     
     
     
     
            execution(t);
            trace(t);
     
     
        }
     
    //partie Execution
        public static void execution (int []t)
        {
            int val;
            int temp=0;
            int dimension2;
            Scanner input = new Scanner(System.in);
            for(int ip = 0 ;ip < dimension2; ip++)
            {
                if(t[ip]==101)
                {
                    ip++;
                    val=t[ip];
                    System.out.println("?");
                    t[(val+dimension2-1)]= input.nextInt();
                }
                if(t[ip]==102)
                {
                    ip++;
                    val=t[ip];
                    System.out.println(t[(val+dimension2-1)]);
                }
                if(t[ip]==103)
                {
                    ip++;
                    val=t[ip];
                    temp=t[(val+dimension2-1)];
                }
                if(t[ip]==104)
                {
                    ip++;
                    val=t[ip];
                    t[(val+dimension2-1)]=temp;
                }
                if(t[ip]==105)
                {
                    ip++;
                    val=t[ip];
                    temp=val;
                }
                if(t[ip]==106)
                {
                    temp++;
                }
                if(t[ip]==107)
                {
                    temp--;
                }
                if(t[ip]==108)
                {
                    ip++;
                    val=t[ip];
                    temp+=val;
                }
                if(t[ip]==109)
                {
                    ip++;
                    val=t[ip];
                    temp += t[(val+dimension2-1)];
                }
                if(t[ip]==110)
                {
                    ip++;
                    val=t[ip];
                    temp-=t[(val+dimension2-1)];
                }
                if(t[ip]==111)
                {
                    ip++;
                    val=t[ip];
                    temp*=t[(val+dimension2-1)];
                }
                if(t[ip]==112)
                {
                    val=t[ip];ip--;
                    ip+=val;
                }
                if(t[ip]==113)
                {
                    ip++;
                    val=t[ip];
                    if(temp==0)
                    {
                        ip=ip-2;
                        ip+=val;
                    }
                }
                if(t[ip]==114)
                {
                    ip++;
                    val=t[ip];
                    if(temp<0)
                    {
                        ip=ip-2;
                        ip=+val;
                    }
                }
                if(t[ip]==115)
                {
                    System.exit(0);
                }
                ip++;
            }
        }
    Quelqu'un saurait-il m'expliquer d'où vient le problème ?

    Merci d'avance pour votre aide.

  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
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public static void execution (int []t)
    {
    int val;
    int temp=0;
    int dimension2;
    Scanner input = new Scanner(System.in);
    for(int ip = 0 ;ip < dimension2; ip++)
    {
    L'erreur parle d'elle-même : Pox.java:149: variable dimension2 might not have been initialized
    Tu fais une boucle sur une taille dimension2 sachant que cet entier n'a même pas été initialisé.
    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 chevronné
    Inscrit en
    Août 2009
    Messages
    1 073
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 1 073
    Points : 1 806
    Points
    1 806
    Par défaut
    Et ça se voit tout de suite, d'ailleurs :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
            int dimension2;
            Scanner input = new Scanner(System.in);
            for(int ip = 0 ;ip < dimension2; ip++)
    Tu souhaites peut-être que "dimension2" soit commune à toutes tes méthodes ? Auquel cas il faudrait ou bien la déclarer en variable de classe, et statique puisque toutes tes méthodes le sont (ce qui est d'ailleurs pas top), ou bien la passer en paramètre de ta méthode.

Discussions similaires

  1. Variables locales : might not have been initialized
    Par cherche_java dans le forum Débuter avec Java
    Réponses: 16
    Dernier message: 07/01/2015, 00h30
  2. Réponses: 2
    Dernier message: 08/10/2012, 20h07
  3. Réponses: 4
    Dernier message: 25/05/2012, 19h31
  4. Que signifie "The local variable A1 may not have been initialized"
    Par lyess dans le forum Débuter avec Java
    Réponses: 12
    Dernier message: 23/05/2011, 17h44
  5. erreur:The local variable may not have been initialized
    Par wiss20000 dans le forum Langage
    Réponses: 7
    Dernier message: 22/03/2007, 15h18

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