Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这里可能出了什么问题?_Java - Fatal编程技术网

Java 这里可能出了什么问题?

Java 这里可能出了什么问题?,java,Java,有人能告诉我这里有什么问题吗 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,

有人能告诉我这里有什么问题吗

    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);
        }
    }
}
import java.io.BufferedInputStream;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.array;
导入java.io.IOException;
公共类TopWordsFinder{
/**
*使用不同的地图,或
*即使是多个数据结构,如果它对您更有意义的话。
*/
Map wordsByLength=新HashMap();
公共静态void main(字符串[]args){
新TopWordsFinder().findTopWords();
}
私有void findTopWords(){
readWords();//将单词读取到数据结构
printTopWords();//从数据结构打印单词
}
//从文件中读取单词并存储在某些数据结构中
//确保在中检查此拼图上下文中单词的定义
//类javadoc
私有无效读字(){
BufferedInputStream in=新的BufferedInputStream(TopWordsFinder.class.getResourceAsStream(“c:/book text.txt”);
StringBuilder word=新的StringBuilder(“”);
试一试{
while(in.available()>0){
char c=(char)in.read();
if(字符isAlphabetic(c)){
if(字符.大写(c)){
c=字符。toLowerCase(c);
}
追加(c);
}否则{
saveWord(word.toString());
word.delete(0,word.length()-1);//重置序列
}
}
}捕获(IOE异常){
System.out.println(“错误:+e”);
}
}
私有void printTopWords(){
整数[]长度=wordsByLength.keySet().toArray(新整数[wordsByLength.keySet().size()]);
数组。排序(长度);
对于(int i=length.length-1;i>=0;i--){
for(字符串字:wordsByLength.get(长度[i]).keySet()){
if(wordsByLength.get(length[i]).get(word)>=3){
System.out.println(“Sõna:+word+”Sõna pikkus:“+word.length()+”kordusi:“+wordsbyllength.get(length[i]).get(word));
返回;
}
}
}
}
专用void保存字(字符串字){
//在此处填写:将单词存储在您选择的数据结构中
if(wordsByLength.get(word.length())==null){
wordsByLength.put(word.length(),new HashMap());
}
if(wordsByLength.get(word.length())==null){
wordsByLength.get(word.length()).put(word,1);
}否则{
int n=wordsByLength.get(word.length()).get(word);
wordsByLength.get(word.length()).put(word,++n);
}
}
}

这是我的java作品,但是我得到了
错误:java.io.IOException:Stream closed

在我看来,您并没有关闭流。我希望这是最后一个街区。。。我找不到这个街区

BufferedInputStream in = new BufferedInputStream(TopWordsFinder.class.getResourceAsStream("c:/book-text.txt"));
差不多

finally
{
    in.close();
}

查看此处了解更多详细信息:

在我看来,您并没有关闭流。我希望这是最后一个街区。。。我找不到这个街区

BufferedInputStream in = new BufferedInputStream(TopWordsFinder.class.getResourceAsStream("c:/book-text.txt"));
差不多

finally
{
    in.close();
}

查看此处了解更多详细信息:

这是您的IO异常

}catch (IOException e) {
  System.out.println("Error: "+e);
 }
您的
char c=(char)in.read()可以抛出它
当流本身损坏或在读取数据期间发生错误时,即安全异常、权限被拒绝等和/或从IOEXception派生的一组异常


为了更好地了解它,您应该在
捕获块中使用
System.out.println(“Error:+e.getMessage())
,这里是IO异常

}catch (IOException e) {
  System.out.println("Error: "+e);
 }
您的
char c=(char)in.read()可以抛出它
当流本身损坏或在读取数据期间发生错误时,即安全异常、权限被拒绝等和/或从IOEXception派生的一组异常


为了更好地了解它,您应该在
捕获块中使用
System.out.println(“Error:+e.getMessage())

我猜问题出在
in.available()
方法中

如果此输入流已关闭,则此方法中会发生IOException 通过调用其close()方法,或发生I/O错误

但你还没有关闭这条小溪。所以我猜问题就在眼前

BufferedInputStream in = new BufferedInputStream(TopWordsFinder.class.getResourceAsStream("c:/book-text.txt"));
检查您的文件路径是否正常,或者此行中是否存在任何其他问题


并在完成任务后始终关闭流(最好在
finally
块中)

我猜问题出在
in.available()
方法中

如果此输入流已关闭,则此方法中会发生IOException 通过调用其close()方法,或发生I/O错误

但你还没有关闭这条小溪。所以我猜问题就在眼前

BufferedInputStream in = new BufferedInputStream(TopWordsFinder.class.getResourceAsStream("c:/book-text.txt"));
检查您的文件路径是否正常,或者此行中是否存在任何其他问题


并在完成任务后始终关闭流(最好在
finally
块中)

我想您可能需要使用扫描仪,这样您的代码更容易阅读

private void readWords() {
  FileInputStream in = new FileInputStream("c:/book-text.txt");
  Scanner scanner = new Scanner(in);
  try {
    while (scanner.hasNext()) {
      String word = scanner.next();
      if (word != null) {
        saveWord(word.toLowerCase());
      } else {
        break;
      }
    }
  } catch (IOException e) {
    System.out.println("Error: " + e.getMessage());
    e.printStackTrace();
  } finally {
    scanner.close();
  }
}

我想您可能想使用扫描仪,这样您的代码更容易阅读

private void readWords() {
  FileInputStream in = new FileInputStream("c:/book-text.txt");
  Scanner scanner = new Scanner(in);
  try {
    while (scanner.hasNext()) {
      String word = scanner.next();
      if (word != null) {
        saveWord(word.toLowerCase());
      } else {
        break;
      }
    }
  } catch (IOException e) {
    System.out.println("Error: " + e.getMessage());
    e.printStackTrace();
  } finally {
    scanner.close();
  }
}
你应使用:-

FileInputStream fin = new FileInputStream("c:/book-text.txt");
BufferedInputStream in = new BufferedInputStream(fin);
你应使用:-

FileInputStream fin = new FileInputStream("c:/book-text.txt");
BufferedInputStream in = new BufferedInputStream(fin);

您从以下try-catch块获得错误消息

}catch (IOException e) {
  System.out.println("Error: "+e);
 }
原因:您没有检查文件是否正确打开。 将打开文件的代码放入try-catch块,如果文件未正确打开:

private void readWords() {
        FileInputStream fis = null;
        BufferedInputStream in = null;

        try {
            fis = new FileInputStream(new File("c:/book-text.txt"));
            in = new BufferedInputStream(fis);
        } catch (Exception e1) {
            e1.printStackTrace();
            return; // unable to open file, so should not proceed further in
                    // readWords
        }
此外,您还需要在
saveWord
方法中修复代码,因为此行的null检查不正确:

if(wordsByLength.get(word.length()) == null){
}
if(wordsByLength.get(word.length()) == null){
} else {}
应该是:

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>());
        wordsByLength.get(word.length()).put(word, 1);
    } else {
        int n = wordsByLength.get(word.length()).get(word);
        wordsByLength.get(word.length()).put(word, ++n);
    }
}
private void保存字(字符串字){
//在此处填写:将单词存储在您选择的数据结构中
if(wordsByLength.get(word.length())==null){
wordsByLength.put(word.length(),new HashMap());
wordsByLength.get(word.length()).put(word,1);
}否则{
int n=wordsByLength.get(word.length())