forked from menault/TD3_DEV51_Qualite_Algo
.
This commit is contained in:
28
word_search.java
Normal file
28
word_search.java
Normal file
@@ -0,0 +1,28 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user