Exception in thread "main" java.lang.Anticonceptionnelle: 4
Résolu
domxaline
-
domxaline -
domxaline -
Bonjour,
en exécutant mon prg il dit que
Exception in thread "main" java.lang.Anticonceptionnelle: 4
normalement il doit afficher
"L'élément n'apparait pas dans ce tableau"
voilà mon output:
Entrez un élément: 4
Entrez un numéros 0: 5
Entrez un numéros 1: 6
Entrez un numéros 2: 7
Entrez un numéros 3: 6
L'élément à chercher: 9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at preOccurrence.occprem(preOccurrence.java:42)
at preOccurrence.main(preOccurrence.java:12)
veuillez m'aidez svp
en exécutant mon prg il dit que
Exception in thread "main" java.lang.Anticonceptionnelle: 4
normalement il doit afficher
"L'élément n'apparait pas dans ce tableau"
voilà mon output:
Entrez un élément: 4
Entrez un numéros 0: 5
Entrez un numéros 1: 6
Entrez un numéros 2: 7
Entrez un numéros 3: 6
L'élément à chercher: 9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at preOccurrence.occprem(preOccurrence.java:42)
at preOccurrence.main(preOccurrence.java:12)
veuillez m'aidez svp
import java.util.Scanner;
public class preOccurrence
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Entrez un élément: ");
int nb=sc.nextInt();
int[]tab=new int[nb];
int[]result=occprem(tab);
if(result!=null)
{
System.out.println("L'élément à chercher 1er occurrance apparait à la position:"+result[0]);
}
}
private static int[] occprem(int[] tab)
{
boolean existe=false;
int p=0;
Scanner sc=new Scanner(System.in);
for(int i=0;i<tab.length;i++)
{
System.out.print("Entrez un numéros "+(i+0)+": ");
tab[i]=sc.nextInt();
}
if(tab.length==0)
{
System.out.println("tableau est vide: ");
}
else
{
System.out.print("L'élément à chercher: ");
int cher=sc.nextInt();
int i=0;
do
{
if(tab[i]==cher)
{
existe=true;
p=i;
}
else
{
i=i+1;
}
}while((i<=tab.length) && (existe==false));
if(existe==true)
{
return new int[]{p};
}
else
{
System.out.print("L'élément n'apparait pas dans ce tableau");
}
}
return tab;
}
}
A voir également:
- Exception in thread "main" java.lang.Anticonceptionnelle: 4
- Deco in paris avis ✓ - Forum Consommation & Internet
- Aux in ✓ - Forum Enceintes / HiFi
- Navigation in private - Guide
- Peut-on brancher un casque sur une prise auxiliaire ? - Forum Casque et écouteurs
- Dc in - Forum Disque dur / SSD
4 réponses
Bonjour,
Ligne 52, vous faites votre do...while sur la condition
Ne devras-ce pas être sur
?
Cordialement
Ligne 52, vous faites votre do...while sur la condition
i<=tab.length
Ne devras-ce pas être sur
i<tab.length
?
Cordialement
en changeant comme ceci:
mon output est:
Entrez un élément: 3
Entrez un numéros 0: 4
Entrez un numéros 1: 5
Entrez un numéros 2: 6
L'élément à chercher: 7
L'élément n'apparait pas dans ce tableauL'élément à chercher 1er occurrance apparait à la position:4
mon output est:
Entrez un élément: 0
tableau est vide:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at PreOccurence1.main(PreOccurence1.java:15)
}while((i<tab.length) && (existe==false));
mon output est:
Entrez un élément: 3
Entrez un numéros 0: 4
Entrez un numéros 1: 5
Entrez un numéros 2: 6
L'élément à chercher: 7
L'élément n'apparait pas dans ce tableauL'élément à chercher 1er occurrance apparait à la position:4
mon output est:
Entrez un élément: 0
tableau est vide:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at PreOccurence1.main(PreOccurence1.java:15)
mais
s'il ne trouve pas élément il s'affiche une erreur
Entrez un élément: 3
Entrez un numéros 0: 4
Entrez un numéros 1: 5
Entrez un numéros 2: 6
L'élément à chercher: 7
L'élément n'apparait pas dans ce tableauL'élément à chercher 1er occurrance apparait à la position:4
il s'affiche aussi la ligne suivante:
L'élément à chercher 1er occurrance apparait à la position:4
je crois que j'ai résolu le pb
à la fin je dois mettre
return tab pour return null
s'il ne trouve pas élément il s'affiche une erreur
Entrez un élément: 3
Entrez un numéros 0: 4
Entrez un numéros 1: 5
Entrez un numéros 2: 6
L'élément à chercher: 7
L'élément n'apparait pas dans ce tableauL'élément à chercher 1er occurrance apparait à la position:4
il s'affiche aussi la ligne suivante:
L'élément à chercher 1er occurrance apparait à la position:4
je crois que j'ai résolu le pb
à la fin je dois mettre
return tab pour return null