présence d'un nom dans un tableau
Bonjour,
but de mon programe est:
Ecrire un programme qui demande à l' utilisateur d' entrer un caractère. Puis le programme affiche tous les noms pré-enregistrés dans le system et commençant par ce caractère. On ne tient pas compte de la casse du caractère (majuscule ou minuscule).
j'ai ecris mon programme ainsi:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
import java.util.Scanner;
public class essaie
{
public static void main (String [] args)
{
String[]tab={"Simon","Chris","David","Sarko","Sego","Hardy","Patrick","Franck","Samir"};
System.out.print("Entrez un nom:");
Scanner sc=new Scanner(System.in);
String c=sc.nextLine();
System.out.println("la liste des noms commençant par "+ c + " est:");
for(int i=0;i<tab.length;i++)
{
if(tab[i].charAt(i)=='s'||tab[i].charAt(i)=='S')
System.out.println(tab[i]);
}
}
} |
je l'ai compilé et exécuté
le resultat n'est pas bon, quelqu'un peut m'aider
Entrez un nom:D
la liste des noms commençant par D est:
Simon
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(Unknown Source)
at essaie.main(essaie.java:14)
Entrez un nom:c
la liste des noms commençant par c est:
Simon
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(Unknown Source)
at essaie.main(essaie.java:14)