Java.lang.ArrayIndexOutOfBoundsException: 4

sikove Messages postés 90 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 31 mars 2010 - 17 avril 2008 à 20:46
 Utilisateur anonyme - 17 avril 2013 à 20:42
salut,

j'ai un probleme bizarre lors d'execution d'un code :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at projettest.main(projettest.java:31)

d'ou ça peut venir svp ?

voila tout mon code java :

////////////////////////////////////////////// capitale ///////////////////////////////////////////////////////////
class capitale extends Ville {
   
    private String president;
   
  public capitale(){
      super();
      president="amine";
  }
 
          public capitale(String nom, int hab, String pays, String president2)
          {
          super(nom, hab, pays);
          this.president = president2;
           }
          
        public String getPresident() {
                return president;
        }
 

        public void setPresident(String president) {
                this.president = president;
        }
       
        public String toString(){
        return super.toString() + " ==>> " + this.president;

           }

 
}

//////////////////////////////////////////////// projettest //////////////////////////////////////////////////

public class projettest {
   
    public static void main(String[] args) {
       

Ville v = new Ville();
Ville v1 = new Ville("marseille", 1236, "france");      
Ville v2 = new Ville("rio", 321654, "brésil");
capitale c=new capitale("test",505,"Maroc","moi");
       
System.out.println(v1.toString());
System.out.println(c.toString());

   Ville tableau[] = new Ville[4];
       
   String[] tab = {"fes1", "fes2", "fes3", "fes4"};
   int[] tab2 = {12356, 78456, 654987, 75835};
        
   /* Les 3 premiers éléments du tableau seront des Villes,
      et le reste, des capitales
   */
   for(int i = 0; i < 6; i++){
     if (i <3){
       Ville k = new Ville(tab[i], tab2[i], "france" );
       tableau[i] = k;
     }
        
     else{
       capitale p = new capitale(tab[i], tab2[i], "france", "Sarko");
       tableau[i] = p;
     }
   }
                
   //il ne nous reste plus qu'à décrire tout notre tableau !
   for(Ville a : tableau){
     System.out.println(a.toString());
   }

    }
}

/////////////////////////////////////////// Ville.java ////////////////////////////////////////
public class Ville {
 
  protected String nomVille;
  protected String nomPays;
  protected int nbreHabitant;
  protected char cat;
 

  public Ville(){
          nomVille = "Inconnu";
          nomPays = "Inconnu";
          nbreHabitant = 0;
          this.setCategorie();
  }
 

  public Ville(String pNom, int pNbre, String pPays)
  { 
          nomVille = pNom;
          nomPays = pPays;
          nbreHabitant = pNbre;
          this.setCategorie();
  }
 
  //************************************************

  public String getNom()
  { return nomVille;}
 

  public String getNomPays()
  { return nomPays; }
 

 public int getNombreHabitant()
 { return nbreHabitant;}
 

 public char getCategorie()
 { return cat;}
 
 //********************************************************

 public void setNom(String pNom)
 { nomVille = pNom;}

 public void setNomPays(String pPays)
 { nomPays = pPays;}
 

public void setNombreHabitant(int nbre)
{
         nbreHabitant = nbre;
         this.setCategorie();
}

//*******************************************************

  private void setCategorie() {
 
      if (this.nbreHabitant < 100)
          this.cat = 'A';
      else
        this.cat = '?';
  }

  public String toString() {

   return this.nomVille+" ville de "+this.nomPays+", elle a "+this.nbreHabitant+
              " => categorie : "+this.cat;
  }
 
}

merci d'avance pour votre aide
A voir également:

14 réponses

Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
17 avril 2008 à 21:09
Salut:

Ici, tu as deux tableaux de longueur 4 et tu as une boucle de 6 itérations, ce qui a déclenché l'exception IndexOutOfBoundsException.

Correction: Il faut ajouter deux éléments à chaque tableau (pour avoir au moins 6 éléments).

String[] tab = {"fes1", "fes2", "fes3", "fes4", "fes5", "fes6"};
int[] tab2 = {12356, 78456, 654987, 75835, 34566, 67543};
0
Rejoignez-nous