CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

[JAVA] Probleme Exception/Scanner

JVCloud
JVCloud
Niveau 10
30 avril 2016 à 19:03:24

Bonjour :-)

J'essaye d'attraper une exception lorsque le mauvais type de données est entré(InputMismatchException) mais lorsque l'exception est d'attrapée, le programme boucle sans me demander d'entrer d'autre valeur

PS: Les autres exceptions fonctionnent tres bien

do{
System.out.println("Entrez les coordonnées de la piéce à utiliser");
try{
x = sc.nextInt();
y = sc.nextInt();
succes=j1.choixPion(x, y, plateau,true);
}
catch(NullPointerException e) {
System.out.println("Saut Impossible : Coordonnees fauses");
succes=false;
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Coordonnees en dehors du tableau" );
succes=false;
}
catch(InputMismatchException e) {
System.out.println("Vous devez entrer un entier");
succes=false;
}
}while(!succes);

Candystand
Candystand
Niveau 10
30 avril 2016 à 20:02:02
while(!succes);

est équivalent à

while(succes == false);

A partir de là, tu peux en déduire d'où vient ton erreur je suppose :)

JVCloud
JVCloud
Niveau 10
30 avril 2016 à 20:05:32

Nan mais c'est pas le probleme(ou en tout cas je crois pas :hap: )

Je veux que ça boucle quand ça chope une exception mais je veux que ça me redemande d'entrer les deux entier a chaque itération

toupine
toupine
Niveau 32
30 avril 2016 à 23:17:56

Intéressant comme problème !

J'ai trouvé ça :

If the next thing to read is not an integer, then nextInt doesn't read anything. So if you type "hi", then nextInt throws an InputMismatchException, and your program calls nextInt again. That call still throws an InputMismatchException, because the next thing to read is still "hi", which isn't an integer. Then your program calls nextInt again, and it throws an exception again for the exact same reason. And so on.

One possib le solution is to call sc.next(), and ignore the result.

http://stackoverflow.com/questions/32042391/confusion-with-an-infinite-while-loop-concerning-scanner-nextint

En gros, la valeur que tu as tapé reste dans le Scanner. Il faut donc ignorer cette valeur en faisant sc.next() dans tes exceptions.

Message édité le 30 avril 2016 à 23:18:17 par toupine
JVCloud
JVCloud
Niveau 10
30 avril 2016 à 23:34:05

Wow merci beaucoup ça fait des heures que je galére :-d

Sous forums
  • Aide à l'achat Mac
  • Steam Deck
  • Création de sites web
  • Création de Jeux
  • Linux
  • Programmation
  • Internet
  • Macintosh
  • Hardware
La vidéo du moment