Ok donc ce code demande à l'utilisateur d'entrer un nom, une année et un genre, et affiche le classement du nom pour l'année sélectionnée. Il lit les données de l'URL et les place dans une carte pour chaque année. Ces cartes sont ensuite placées dans un tableau, et finalement le rang est affiché à l'utilisateur. Mon programme ne finit jamais de courir après avoir entré la boucle while. Voici le code:Pourquoi cette boucle for s'exécute-t-elle indéfiniment après l'entrée de réception? (Java)
public static void main(String[] args) throws MalformedURLException, IOException {
Scanner input = new Scanner(System.in);
Map[] boyArray = new Map[10];
Map[] girlArray = new Map[10];
System.out.print("Enter year (between 2001 and 2010): ");
String year = input.nextLine();
System.out.print("Enter gender (M or F): ");
String gender = input.nextLine();
System.out.print("Enter name: ");
String name = input.nextLine();
for (int i = 0; i < 10; i++) {
Map<String, String> boys = new HashMap<>();
Map<String, String> girls = new HashMap<>();
try {
java.net.URL url = new java.net.URL("http://www.cs.armstrong.edu/liang/data/babynamesranking"+ year + ".txt");
Scanner urlInput = new Scanner(url.openStream());
while (urlInput.hasNext()) {
String rank = input.next();
boys.put(urlInput.next(), rank);
urlInput.next();
girls.put(input.next(), rank);
urlInput.next();
}
}
catch (IOException ex) {
}
boyArray[i] = boys;
girlArray[i] = girls;
}
if (gender.charAt(0) == 'M') {
System.out.println("The name " + name + " was ranked " + boyArray[Integer.parseInt(year) - 2001].get(name) + " in " + year);
} else {
System.out.println("The name " + name + " was ranked " + girlArray[Integer.parseInt(year) - 2001].get(name) + " in " + year);
}
}
}
Chaîne rank = input.next(); <- semble que vous voulez urlInput. La même chose s'applique à girls.put (input.next(), rank); – uthark
Wow, merci beaucoup! Je ne peux pas croire que je n'ai pas remarqué ça. J'ai été coincé dessus pendant des heures lol. Ça fonctionne maintenant! – macdaddy