比较单词

时间:2014-11-28 21:13:30

标签: java compare

我的代码有问题。我在文本中编写了计数单词的程序,但我的模式问题很小,必须在文本中搜索。也许有人可以帮助我。

import java.util.*;

class KomparatorLicz implements Comparator<Word> {

@Override
public int compare(Word arg0, Word arg1) {
return arg1.amount - arg0.amount;
}

}

class KomparatorString implements Comparator<Word> {

@Override
public int compare(Word obj1, Word obj2) {
  if (obj1.content == obj2.content) {
      return 0;
  }
  if (obj1.content == null) {
      return -1;
  }
  if (obj2.content == null) {
      return 1;
  }
  return obj1.content.compareTo(obj2.content);
}

}

class Word { public String content; public int amount;

public Word(String content, int amount) {
this.content = content;
this.amount = amount;
}

@Override
public String toString() {
return "Word [content=" + content + ", amount=" + amount + "]";
}

}

public class Source4 {

public static float procent(int wordCount, int oneWord)
{
return (((float)oneWord*100)/(float)wordCount);
}

public static void main(String[] args) {
String line, wordsLine[];
    String klucze = null;
int valTemp;
int wordCount=0;
int keyWords=0;
HashMap<String, Word> slownik = new HashMap<String, Word>();
ArrayList<Word> lista= new ArrayList<Word>();
ArrayList<Object> keyWordsList = new ArrayList<Object>();
Scanner in = new Scanner(System.in);
String alph = in.nextLine();

keyWords = in.nextInt();

    for(int i=0; i<keyWords; i++)
    {
        klucze = in.next();
        keyWordsList.add(klucze);
    }



while(in.hasNextLine())
{       
   line = in.nextLine();

   if(line.equals("koniec")) break;

   wordsLine  = line.split("[^" + alph + "]");

   for(String s : wordsLine) {
       if(s != null && s.length() > 0) 
       {           
       wordCount++;
       if(slownik.containsKey(s))
       {
           valTemp = slownik.get(s).amount;
           slownik.remove(s);
           valTemp++;
           slownik.put(s, new Word(s,valTemp));
       }
       else
       {
           slownik.put(s, new Word(s,1));
       }
       }
    }
}

for (String key : slownik.keySet()) 
{
    lista.add(slownik.get(key));
}



Collections.sort(lista, new KomparatorString());

StringBuffer result = new StringBuffer();
int keyWordCounter=0;
int amountBuff=0;
float percentBuff=0;

for (int i = 0; i<lista.size();i++) 
{
    if(keyWordsList.contains(lista.get(i)))
    {
    result.append(amountBuff+" "+percentBuff+"%");
    amountBuff = 0;
    percentBuff = 0; 
    result.append("\n");
    result.append(lista.get(i).amount+" "+(procent(wordCount,lista.get(i).amount)+"%"));
    result.append(" "+lista.get(i).content);
    result.append("\n");
    keyWordCounter+=lista.get(i).amount;
    }
    else
    {
    amountBuff+=lista.get(i).amount;
    percentBuff+=procent(wordCount,lista.get(i).amount);
    }
}
result.append(amountBuff+" "+percentBuff+"%");
System.out.println("Wersja AK");
System.out.println(keyWords+" różnych słów kluczowych");
System.out.println(wordCount+" wystąpień wszystkich słów");
System.out.println(keyWordCounter+" "+procent(wordCount,keyWordCounter)+"% "+" wystąpień słów kluczowych");
System.out.println((wordCount-keyWordCounter)+" "+procent(wordCount,(wordCount-keyWordCounter))+"% wystąpień innych słów");
System.out.println(result);
}

}

1 个答案:

答案 0 :(得分:0)

这是错误的代码if(keyWordsList.contains(lista.get(i)))

您需要if(keyWordsList.contains(lista.get(i).content))