Bon, et maintenant, la version telle que je l'aurais écrite:
Code :
import java.lang.String; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class ScanWord { public static void main (String[] args ) { System. out. print("Enter the sentence: " ); Scanner inputScanner = new Scanner (System. in); String input = inputScanner. nextLine(). trim(); if (input.length() == 0) { inputScanner.close(); System. out. println("Sentence entered is empty!" ); } else { System. out. print("Enter the word: " ); // Note: On pourrait déclencher une erreur s'il y a plus d'un mot String word = inputScanner. nextLine(). trim(). split("\\s+" )[0]; inputScanner.close(); if (word.length() == 0) { System. out. println("Word entered is empty!" ); } else { List<String> wordList = Arrays. asList(input. trim(). split("\\s+" )); int occurences = 0; // Note: ou equalsIgnoreCase selon ce qu'on veut if (w.equals(word)) { ++occurences; } } switch (occurences) { case 0: System. out. println("no \"" + word + "\" in the sentence." ); break; case 1: System. out. println("\"" + word + "\" occurs once in the sentence." ); break; default: System. out. println("\"" + word + "\" occurs " + occurences + " times in the sentence." ); break; } } } } }
|
et la même chose avec des commentaires explicatifs:
Code :
import java.lang.String; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class ScanWord { public static void main (String[] args ) { // D'abord, on récupère les données en entrée System. out. print("Enter the sentence: " ); Scanner inputScanner = new Scanner (System. in); // On récupére l'entrée comme une String: inputScanner.nextLine(), // On vire les blancs autour: trim() String input = inputScanner. nextLine(). trim(); if (input.length() == 0) { inputScanner.close(); System. out. println("Sentence entered is empty!" ); } else { System. out. print("Enter the word: " ); // On récupére l'entrée et on ne garde que le premier mot // Note: On pourrait déclencher une erreur s'il y a plus d'un mot String word = inputScanner. nextLine(). trim(). split("\\s+" )[0]; inputScanner.close(); if (word.length() == 0) { System. out. println("Word entered is empty!" ); } else { // avec input on construit un String[] en séparant chaque mot: split("\\s+" ) // On transforme cela en List<String>: Arrays.asList(String[]) // Bref, on transforme l'entrée en une liste de mots List<String> wordList = Arrays. asList(input. trim(). split("\\s+" )); // On parcourt la liste pour comparer chaque mot a celui a trouver int occurences = 0; // Note: ou equalsIgnoreCase selon ce qu'on veut if (w.equals(word)) { ++occurences; } } // On écrit le résultat switch (occurences) { case 0: System. out. println("no \"" + word + "\" in the sentence." ); break; case 1: System. out. println("\"" + word + "\" occurs once in the sentence." ); break; default: System. out. println("\"" + word + "\" occurs " + occurences + " times in the sentence." ); break; } } } } }
|
Note:
Il y avait moyen de faire sans passer par les listes et en restant avec des arrays de strings:
Code :
import java.lang.String; import java.util.Scanner; public class ScanWord { public static void main (String[] args ) { System. out. print("Enter the sentence: " ); Scanner inputScanner = new Scanner (System. in); String input = inputScanner. nextLine(). trim(); if (input.length() == 0) { inputScanner.close(); System. out. println("Sentence entered is empty!" ); } else { System. out. print("Enter the word: " ); // Note: On pourrait déclencher une erreur s'il y a plus d'un mot String word = inputScanner. nextLine(). trim(). split("\\s+" )[0]; inputScanner.close(); if (word.length() == 0) { System. out. println("Word entered is empty!" ); } else { String[] wordList = input. trim(). split("\\s+" ); int occurences = 0; for (int i = 0; i < wordList.length; ++i) { // Note: ou equalsIgnoreCase selon ce qu'on veut if (wordList[i].equals(word)) { ++occurences; } } switch (occurences) { case 0: System. out. println("no \"" + word + "\" in the sentence." ); break; case 1: System. out. println("\"" + word + "\" occurs once in the sentence." ); break; default: System. out. println("\"" + word + "\" occurs " + occurences + " times in the sentence." ); break; } } } } }
|
Mais bon, autant choisir une structure de données (Liste) qui reflète bien ce que l'on a en tête.
A+,
Message édité par gilou le 20-05-2012 à 16:01:09
---------------
There's more than what can be linked! -- Le capitaine qui ne veut pas obéir à la carte finira par obéir aux récifs. -- Il ne faut plus dire Sarkozy, mais Sarkozon -- (╯°□°)╯︵ ┻━┻