public static Map<String, Integer> countWords(ArrayList<String> list) {
HashMap<String, Integer> result = new HashMap<String, Integer>();
int count = 0;
for (int i = 0; i < list.size() ; i++) {
for (int j = i + 1; j < list.size(); j++) {
String word = list.get(i);
String anotherWord = list.get(j);
if (word != null) {
if (word == anotherWord){
count++;
result.put(word, count);
} else if (word != anotherWord){
count++;
result.put(word,count);
}
}
}
Валидатор не пропускает, сдаётся мне, что я перемудрил с кодом. В чём ошибка?package com.javarush.task.task10.task1016;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/*
Одинаковые слова в списке
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> words = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
words.add(reader.readLine().toLowerCase(Locale.ROOT));
}
Map<String, Integer> map = countWords(words);
for (Map.Entry<String, Integer> pair : map.entrySet()) {
System.out.println(pair.getKey() + " " + pair.getValue());
}
}
public static Map<String, Integer> countWords(ArrayList<String> list) {
HashMap<String, Integer> result = new HashMap<String, Integer>();
int count = 1;
for (int i = 0; i < list.size() ; i++) {
for (int j = i + 1; j < list.size(); j++) {
String word = list.get(i);
String anotherWord = list.get(j);
if (word != null) {
if (word == anotherWord){
count++;
result.put(word, count);
} else if (word != anotherWord){
count++;
result.put(word,count);
}
}
}
}
return result;
}
}