使用

时间:2019-10-23 21:25:13

标签: java

我有这个课程,我从文件中读取数据,程序添加了单词,并计算了每个单词,例如: (WData {word =成功,count = 1} WData {word =自动机,count = 1} Data {word = theory,count = 2})如何排序输出并以字符A到Z开头,我是指单词and。这是我的代码       Sa类

    class SA:
public class SA<T> {
int head;
int size;
T[] nodes;
public SA(int maxsize) {
head = 0;
nodes = (T[]) new Object[maxsize];
size = 0;
}
public int size() {
return size;}
public boolean empty() {
return size == 0;
   public void insert(T e) {
   nodes[size] = e;
 size++;
 }
 public T find(T s) {

if (size != 0) {
for (int i = 0; i < size; i++) {
    if (nodes[i].equals(s)) {
        return nodes[i];
    }
}
 }
  return null;
  }
  public void display() {
 for (int i = 0; i < size; i++) {
  System.out.println(nodes[i].toString());
  }}}

这里是TextAnalyzer类:

Public class TextAnalyzer<T> {
private static class WData {

@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + Objects.hashCode(this.word);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
    return true;
}
if (obj == null) {
    return false;
}
if (getClass() != obj.getClass()) {
    return false;
}
final WData other = (WData) obj;
if (!Objects.equals(this.word, other.word)) {
    return false;
}
return true;
}

String word;
int count;

public WData(String w, int f) {
 word = w;
 count = f;
 }
@Override
public String toString() {
return "WData{" + "word=" + word + ", count=" + count + '}';
}

} SA<WData>list;
public TextAnalyzer() {
list=new SA<WData>(11000); 
}
public void processText(String filename) {
long count = 0;
try {

Scanner sc = new Scanner(new File(filename));;

while (sc.hasNext()) {
    String line = sc.next();
    String st = line.replaceAll("[^a-zA-Z ]", "").toLowerCase();

    processWord(st);}
  list.display();

} catch (FileNotFoundException ex) {
System.out.println("There are error in processText");
}
}
public void processWord(String word) {
WData w = new WData(word, 1);
WData s = list.find(w);
if (s == null) {

list.insert(w);

 } else {
s.count = s.count + 1;
}}}

0 个答案:

没有答案