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
|
import java.io.*;
import java.util.Scanner;
public class MenuBis {
Scanner sc = new Scanner(System.in);
String[]rubriques;
MenuBis(String[]tabRubriques){ // constructeur
this.rubriques=tabRubriques;
}
private void afficher(){
System.out.println("Saisir les rubriques = ");
String []tabRubriques=new String[7]; // rubriques dans le tabRubriques
for(int i=0; i<tabRubriques.length;i++){
tabRubriques[i]=sc.nextLine(); //j'ecris les rubriques
}
for(int i=0; i<tabRubriques.length;i++){ // afficher tabRubriques avec l'indice commençant à 1
System.out.println((i+1) +"- " + tabRubriques[i]);
}
}
private int choix()throws PasNbre,HorsTableau {
System.out.println("Entrer votre choix = ");
int choix=sc.nextInt();
sc.nextLine();
String conv= "" + choix; //je convertie en string
char ck=conv.charAt(0);
boolean rep=Character.isDigit(ck); //si c'est un chiffre,true
if(!rep)
throw new PasNbre(); //c 'est pas un chiffre
if ( choix<1 || choix>this.rubriques.length )
throw new HorsTableau();
return choix;
}
class PasNbre extends Exception{}
class HorsTableau extends Exception{}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String []tabRubriques=new String[7];
MenuBis menuRubriques= new MenuBis(tabRubriques);
boolean bonChoix=true;
menuRubriques.afficher();
do{
try{
menuRubriques.choix();
}
catch(PasNbre e){
System.out.println("Vous n'avez pas saisi un nombre");
System.out.println("Recommencez !");
bonChoix=false;
}
catch(HorsTableau e){
System.out.println("Votre choix doit être compris entre 1 et 7");
System.out.println("Recommencez,");
bonChoix=false;
}
}
while(!bonChoix);
}
} |