Может ли кто-нибудь дать мне, что здесь может быть не так?Что здесь может быть неправильным?
import java.io.BufferedInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.io.IOException;
public class TopWordsFinder {
/**
* Map of <word length, <word content, count in text>> Use some different, or
* even multiple data structures if it makes more sense to you.
*/
Map<Integer, Map<String, Integer>> wordsByLength = new HashMap<>();
public static void main(String[] args) {
new TopWordsFinder().findTopWords();
}
private void findTopWords() {
readWords(); // Read words to data structure
printTopWords(); // Print words from data structure
}
// Reads words from file and stores in some data structure
// Make sure to check the definition of word in context of this puzzle in
// class javadoc
private void readWords() {
BufferedInputStream in = new BufferedInputStream(TopWordsFinder.class.getResourceAsStream("c:/book-text.txt"));
StringBuilder word = new StringBuilder("");
try{
while (in.available() > 0) {
char c = (char) in.read();
if(Character.isAlphabetic(c)){
if(Character.isUpperCase(c)){
c = Character.toLowerCase(c);
}
word.append(c);
}else{
saveWord(word.toString());
word.delete(0, word.length()-1);// Reset the sequence
}
}
} catch (IOException e) {
System.out.println("Error: "+e);
}
}
private void printTopWords() {
Integer[] lengths = wordsByLength.keySet().toArray(new Integer[wordsByLength.keySet().size()]);
Arrays.sort(lengths);
for(int i = lengths.length-1; i>=0; i--){
for(String word : wordsByLength.get(lengths[i]).keySet()){
if(wordsByLength.get(lengths[i]).get(word) >= 3){
System.out.println("Sõna: "+word+" sõna pikkus: "+word.length()+" kordusi: "+wordsByLength.get(lengths[i]).get(word));
return;
}
}
}
}
private void saveWord(String word) {
// FILL IN HERE: Store word in data structure you chose
if(wordsByLength.get(word.length()) == null){
wordsByLength.put(word.length(), new HashMap<String, Integer>());
}
if(wordsByLength.get(word.length()) == null){
wordsByLength.get(word.length()).put(word, 1);
}else{
int n = wordsByLength.get(word.length()).get(word);
wordsByLength.get(word.length()).put(word, ++n);
}
}
}
Это моя ява работа, но я получаю Error: java.io.IOException: Stream closed
.
Пожалуйста, разместить код здесь, а не ссылку на него, а также объяснить, что именно эта проблема. – SudoRahul
Я не знаю, как публиковать обычный текст здесь –
Где вы можете получить эту ошибку? – Hitman