繁体   English   中英

如何解决错误:找不到符号

[英]how to fix error: cannot find symbol

我感觉好像缺少了一些非常简单的东西,但是找不到。 这段代码的目的是获取一个莎士比亚文件,并使用一个哈希图来查找文本给出的单词次数以及“ n”个字符的单词长度。 但是我什至无法进入调试部分,因为我得到了错误

Bard.java:13: error: cannot find symbol
     Pattern getout = Pattern.compile("[\\w']+"); //this will take only the words
     ^   symbol:   class Pattern   location: class Bard
Bard.java:13: error: cannot find symbol
     Pattern getout = Pattern.compile("[\\w']+"); //this will take only the words

再加上一些位置。 帮助将不胜感激。

import java.io.*;
import java.util.*;

public class Bard {

  public static void main(String[] args) {

    HashMap < String, Integer > m1 = new HashMap < String, Integer > (); // sets the hashmap

    //create file reader for the shakespere text
    try (BufferedReader br = new BufferedReader(new FileReader("shakespeare.txt"))) {
      String line = br.readLine();
      Pattern getout = Pattern.compile("[\\w']+"); //this will take only the words 

      //create the hashmap
      while (line != null) {
        Matcher m = getout.matcher(line); //find the relevent information

        while (m.find()) {
          if (m1.get(m.group()) == null && !m.group().toUpperCase().equals(m.group())) { //find new word that is not in all caps.
            m1.put(m.gourp(), 1);
          } else { //increments the onld word
            int newValue = m1.get(m.group());
            newValue++;
            m1.put(m.group, newValue);
          }
        }
        line = br.readLine();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }


    try (BufferedReader br2 = new BufferedReader(new FileReader("input.txt"))) {
      String line2 = br2.readLine();
      FileWriter output = new FileWriter("analysis.txt");

      while (line2 != null) {

        if (line2.matches("[\\d\\s]+")) { // if i am dealing with the two integers 
          String[] args = line.split(" "); // split them up
          wordSize = Integer.parseInt(args[0]); // set the first on the the word size
          numberOfWords = Integer.parseInt(args[1]); // set the other one to the number of words wanted
          String[] wordsToReturn = new String[numberOfWords]; //create array to place the words
          int i = 0;
          int j;
          for (String word: m1.keySet()) { //  
            if (word.length() == wordSize) {
              wordToReturn[i] = word;
              i++;
            }


            for (j = 0; numberOfWords > j; j++) {
              output.write(wordToReturn[j]);
            }
          }
        } else {
          output.write(m1.get(line2));
        }


      }
      line2 = br2.readLine();
    } catch (Exception e) {
      e.printStackTrace();
    }

  }
}

您尚未导入Pattern类。 用:-导入

import java.util.regex.*;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM