Bonjour,
je suis débutant en programmation Java, j'ai une question qui concerne un programme permettant de renvoyer les mots présents dans un texte ainsi que leurs occurrences, en les triant par ordre décroissant; mon code est le suivant :
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
package entrainementTPNote2;
 
import java.util.Arrays;
 
public class UtilisationCollections {
	static String texte = "bonjour je m'appelle gabriel enriquez et je suis"
			+ " très nul en java et en programmation en général en général";
 
	public static void main(String[] args) {
		TableauDeMots();
	}
 
	public static void TableauDeMots() {
		String[] TableauDeMots = texte.split(" ");
		System.out.println(Arrays.toString(TableauDeMots));
		int[] cpt = new int[TableauDeMots.length];
		for (int k = 0; k < TableauDeMots.length; k++) {
			cpt[k] = 1;
			for (int j = k+1; j < TableauDeMots.length; j++) {
				if ((String)TableauDeMots[j] == (String)TableauDeMots[k]) {
					cpt[k]++;
					cpt[j]=0;
				}
			}
		}
		Arrays.sort(cpt);
		for (int i = 0; i < cpt.length; i++) {
			if (cpt[i] != 0) {
				System.out.println("Le mot " + TableauDeMots[i] + " est répété " + cpt[i] + " fois");
			}
		}
	}
}
Le problème est que quand j’exécute le programme, il affiche dans la console :
[bonjour, je, m'appelle, gabriel, enriquez, et, je, suis, très, nul, en, java, et, en, programmation, en, général, en, général]
Le mot bonjour est répété 1 fois
Le mot je est répété 1 fois
Le mot m'appelle est répété 1 fois
Le mot gabriel est répété 1 fois
Le mot enriquez est répété 1 fois
Le mot et est répété 1 fois
Le mot je est répété 1 fois
Le mot suis est répété 1 fois
Le mot très est répété 1 fois
Le mot nul est répété 1 fois
Le mot en est répété 1 fois
Le mot java est répété 1 fois
Le mot et est répété 1 fois
Le mot en est répété 1 fois
Le mot programmation est répété 1 fois
Le mot en est répété 1 fois
Le mot général est répété 1 fois
Le mot en est répété 1 fois
Le mot général est répété 1 fois;
Il ne prend pas en compte le fait qu'il faille afficher le nombre d'occurrences de chaque mot et afficher chaque mot une seule fois.
Pouvez-vous m'aider s'il vous plaît, merci d'avance pour votre réponse.