forked from menault/TD3_DEV51_Qualite_Algo
28 lines
1.1 KiB
Java
28 lines
1.1 KiB
Java
import java.util.*;
|
|
import java.io.*;
|
|
|
|
public class word_search {
|
|
public static String getRandomWord() {
|
|
List<String> words = new ArrayList<>(); // Liste dynamique de mots
|
|
Random random = new Random();
|
|
try { //lis le fichier, regarde si il existe avant de sélectionner un mot aléatoire
|
|
File file = new File("francais.txt");
|
|
Scanner scanner = new Scanner(file, "UTF-8");
|
|
while (scanner.hasNextLine()) {
|
|
String line = scanner.nextLine().trim();
|
|
if (!line.isEmpty()) { // ignore les lignes vides
|
|
words.add(line.toLowerCase());
|
|
}
|
|
}
|
|
scanner.close();
|
|
if (words.isEmpty()) {
|
|
System.out.println(" Aucun mot trouvé dans le fichier français.txt !");
|
|
return "erreur";
|
|
}
|
|
return words.get(random.nextInt(words.size()));
|
|
} catch (FileNotFoundException e) {
|
|
System.out.println(" Fichier 'français.txt' introuvable !");
|
|
return "erreur";
|
|
}
|
|
}
|
|
} |