Bonjour
Je suis de faire un programme qui permet de comptabiliser le nombre d'occurrence d'un mot à partir d'un fichier texte.
Je stocke chaque mot dans un HashMap avec la clé = mot et la valeur =nbre d'apparition.
Je lance mon programme et ça fonctionne mais il ya un message d'erreur
Le mot lady figure 4 fois
si quelqu'un peut m'aider à résoudre ce problème.Exception in thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796)
at java.util.HashMap$EntryIterator.next(HashMap.java:834)
at java.util.HashMap$EntryIterator.next(HashMap.java:832)
at tp3javaEx2.Mots.afficherMap(Mots.java:60)
at tp3javaEx2.Main.main(Main.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Voici mon code de remplissage et de l'affichage de la map
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 public void remplirMap(){ try { f=new FileWordReader("story.txt"); }catch (FileNotFoundException e){ System.out.println ("KO! File not found!"); } String word = f.getNextWord(); while (word != null){ if(m.containsKey(word)){ nbreutilisation+=1; } else{ nbreutilisation=1;m.put(word, nbreutilisation); } m.put(word,nbreutilisation); word=f.getNextWord(); } } public void afficherMap(){ Set<Map.Entry<String,Integer>> entre = m.entrySet(); Iterator<Map.Entry<String,Integer>> iter=entre.iterator(); while(iter.hasNext()){ String mot = (String)iter.next().getKey(); Integer nbredefois=(Integer)iter.next().getValue(); System.out.println("Le mot " + mot + " figure " +nbredefois + " fois"); } }
Partager