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

Collection et Stream Java Discussion :

Array Out Of BoundsException:0


Sujet :

Collection et Stream Java

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2010
    Messages : 11
    Points : 7
    Points
    7
    Par défaut Array Out Of BoundsException:0
    Bonjour à tous,

    J'ai un problème qui dur depuis 24h : je veux faire en sorte que l'utilisateur decide lui même de la taille de la combinaison mastermind( Attention : ce n'est pas du graphique) . Seul problème lorsque je lance le main (deuxième fragment de code) : il me demande de rentrer la taille de la combinaison je la rentre et la j'ai cette erreur:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Mastermind.genererCombinaison(Mastermind.java:23)
    at Mastermind.<init>(Mastermind.java:16)
    at Main.main(Main.java:7)

    Pouvez vous m'aidez? Voici les 2 codes:

    Mastermind.java:
    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
    import java.lang.Math;
    import java.util.Random;
     
    public class Mastermind {
    	private int tailleCombi;
    	private Random rnd =new Random();
    	private int[] combinaison = new int [this.tailleCombi];
     
    	public Mastermind(){
    		this.tailleCombi = 5;
    		genererCombinaison();
    	}
     
    	public Mastermind(int taille){
    		this.tailleCombi = taille;
    		genererCombinaison();
    	}
     
    	public void genererCombinaison(){
    		for (int i=0;i < this.tailleCombi;i++){
    			int choix = rnd.nextInt(tailleCombi);
    			System.out.println(choix);
    			combinaison[i]=choix;
    		}
    	}
     
    		public int nbBienplaces (int[] tabChiffres){
    			int compteur=0;
    			int j=0;
    			for (int i=0;i<this.tailleCombi;i++){
    				if (combinaison[i]== tabChiffres[j]){
    					compteur++;
    				}
    				j++;
    			}
    			return compteur;
    		}
     
    		public int nbMalplaces (int[] tabChiffres){
    			int[] tabTemp = new int [this.tailleCombi];
    			tabTemp = (int[]) this.combinaison.clone();
    			int compteur = 0;
    			for (int k = 0 ; k < this.tailleCombi ; k++){
    				for (int j = 0 ; j < this.tailleCombi ; j++){
    					if(tabTemp[j] != -1){
    						if (tabChiffres[k] == tabTemp[j]){
    							if(tabChiffres[k] != tabTemp[k]){
    								compteur++;
    								tabTemp[j] = -1;
    								j = 5;
    							}
    							else{
    								tabTemp[k] = -1;
    								j = 5;
    							}
    						}
    					}
    				}
    			}
    			return compteur;
    		}
     
    		public int[] getCombinaison() {
    			return combinaison;
    		}
    }
    Main.java:
    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
    import java.util.Scanner;
     
    public class Main {
    	public static void main (String [] args){
    		System.out.println("Rentrez la taille de la combinaison : ");
    		Scanner s = new Scanner(System.in);
    		Mastermind master = new Mastermind(s.nextInt());
    		int[] tabChiffres = new int[5];
     
    		System.out.println("Veuillez saisir votre combinaison");
    		Scanner sc = new Scanner(System.in);
    		for (int i=0;i<5;i++){
    			tabChiffres[i]= sc.nextInt();
    		}
    		System.out.println("Nbde chiffres bien places");
    		System.out.println(master.nbBienplaces(tabChiffres));
    		System.out.println("Nb de chiffres mal places");
    		System.out.println(master.nbMalplaces(tabChiffres));
    	}
    }

  2. #2
    Membre confirmé
    Avatar de william44290
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Juin 2009
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 400
    Points : 575
    Points
    575
    Par défaut
    Je peux pas tester, mais je ferais plutôt cela :

    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
    public class Mastermind {
    	private int tailleCombi;
    	private Random rnd =new Random();
    	private int[] combinaison = new int [this.tailleCombi];
    	private int[] combinaison;
    
    	public Mastermind(){
    		this.tailleCombi = 5;
                    combinaison = new int [this.tailleCombi];
    		genererCombinaison();
    	}
    	
    	public Mastermind(int taille){
    		this.tailleCombi = taille;
                    combinaison = new int [this.tailleCombi];
    		genererCombinaison();
    	}
     
    	public void genererCombinaison(){
    		for (int i=0;i < this.tailleCombi;i++){
    			int choix = rnd.nextInt(tailleCombi);
    			System.out.println(choix);
    			combinaison[i]=choix;
    		}
    	}
     
    		public int nbBienplaces (int[] tabChiffres){
    			int compteur=0;
    			int j=0;
    			for (int i=0;i<this.tailleCombi;i++){
    				if (combinaison[i]== tabChiffres[j]){
    					compteur++;
    				}
    				j++;
    			}
    			return compteur;
    		}
    		
    		public int nbMalplaces (int[] tabChiffres){
    			int[] tabTemp = new int [this.tailleCombi];
    			tabTemp = (int[]) this.combinaison.clone();
    			int compteur = 0;
    			for (int k = 0 ; k < this.tailleCombi ; k++){
    				for (int j = 0 ; j < this.tailleCombi ; j++){
    					if(tabTemp[j] != -1){
    						if (tabChiffres[k] == tabTemp[j]){
    							if(tabChiffres[k] != tabTemp[k]){
    								compteur++;
    								tabTemp[j] = -1;
    								j = 5;
    							}
    							else{
    								tabTemp[k] = -1;
    								j = 5;
    							}
    						}
    					}
    				}
    			}
    			return compteur;
    		}
     
    		public int[] getCombinaison() {
    			return combinaison;
    		}
    }

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2010
    Messages : 11
    Points : 7
    Points
    7
    Par défaut Merci
    Merci ! Cela marche parfaitement

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2010
    Messages : 11
    Points : 7
    Points
    7
    Par défaut Deuxieme problème
    Mon programme ne traite plus les combinaisons , voici la ligne qui plante:elle se trouve dans le main . Help

    tabChiffres[i]= sc.nextInt();

    Edi: c'est bon c'est la taille de tabchiffres qui est pas bien initialiser

  5. #5
    Membre confirmé
    Avatar de william44290
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Juin 2009
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 400
    Points : 575
    Points
    575
    Par défaut
    montrer le message d'erreur et le code.

Discussions similaires

  1. Array index out of range: 0
    Par Morji2810 dans le forum Collection et Stream
    Réponses: 12
    Dernier message: 02/08/2013, 11h09
  2. [OL-2007] Erreur Array out of bounds lors d'une boucle For .. To ..
    Par IronBibs dans le forum VBA Outlook
    Réponses: 2
    Dernier message: 24/11/2009, 09h49
  3. Réponses: 1
    Dernier message: 05/08/2008, 18h27
  4. [Struts]Bean populate & array index out of bound
    Par djoukit dans le forum Struts 1
    Réponses: 7
    Dernier message: 02/11/2006, 11h03
  5. [Débutant]pb de fichier - array index out of bounds exception
    Par TheBlue dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 12/06/2006, 20h24

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