Salut,
Pourquoi changer ton tableau?
String[]tab={"simon","Chris","David","Sarko","Sego","Hardy","Patrick","Franck","Samir"};
C'est toi qui crée ce tableau donc tu dois avoir tous les noms qui commencent par une majuscule (pourquoi changer la règle du jeu?)
Alors conserve
1 2
| Scanner sc=new Scanner(System. in);
String c=sc.nextLine().toUpperCase(); |
Pour gérer le cas sans noms trouvés:
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
| 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();
c = c.toUpperCase(); // ajouter
boolean pasDeNom = true; // ajouter
//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')
if(tab[i].startsWith(c))
{ // ajouter
// ajouter les 2 lignes qui suivent
if (pasDeNom == true)
System.out.println("la liste des noms qui commencent par " + c + " est:");
System.out.println(tab[i]);
pasDeNom = false; // ajouter
} //ajouter
}
// ajouter
if (pasDeNom == true)
System.out.println("Il n'y a pas de noms qui commencent par " + c);
} |
Cordialement,
Dan
Partager