Bonsoir,
J'essaie de gérer des erreurs de saisie: si ce n'est pas un entier et si c'est en dehors du tableau. Je n'est pas d'erreur à la compilation mais j'ai quand même 2 erreurs:
quand je saisie une lettre, il m'affiche
2Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Menu.choix(Menu.java:21)
at Menu.main(Menu.java:44)
et quand je saisie un chiffre en dehors du tableau, il me redemande bien de ressaisir mais si je fais une saisie correct, il me demande encore de ressaisir, je ne sors plus de mon do.
Si quelqu'un à une idée....Merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.io.*; import java.util.Scanner; public class Menu { Scanner sc = new Scanner(System.in); String[]rubriques; Menu(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(); 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]; Menu menuRubriques= new Menu(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){ Terminal.ecrireStringln("Votre choix doit être compris entre 1 et 7"); Terminal.ecrireStringln("Recommencez,"); bonChoix=false; } } while(!bonChoix); } }
Partager