Java 字斟句酌

Java 字斟句酌,java,compare,Java,Compare,我的代码有问题。我写了一个计算文本中单词数的程序,但我对文本中必须搜索的模式有一个小问题。也许有人能帮我 import java.util.*; 类KomparatorLicz实现比较器{ } 类KomparatorString实现比较器{ } 类字 { 公共字符串内容; 公共整数金额; public Word(String content, int amount) { this.content = content; this.amount = amount; } @Override pub

我的代码有问题。我写了一个计算文本中单词数的程序,但我对文本中必须搜索的模式有一个小问题。也许有人能帮我

import java.util.*;
类KomparatorLicz实现比较器{

}

类KomparatorString实现比较器{

}

类字
{
公共字符串内容;
公共整数金额;

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

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

公共类源4{

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);
}
publicstaticfloatprocent(int-wordCount,int-oneWord)
{
返回(((浮点)一个字*100)/(浮点)字数);
}
公共静态void main(字符串[]args){
字符串行,wordsLine[];
字符串klucze=null;
int valTemp;
int字数=0;
int关键字=0;
HashMap slownik=新HashMap();
ArrayList lista=新的ArrayList();
ArrayList关键字列表=新建ArrayList();
扫描仪输入=新扫描仪(系统输入);
字符串alph=in.nextLine();
关键词=in.nextInt();
对于(int i=0;i 0)
{           
字数++;
if(slownik.containsKey(s))
{
valTemp=slownik.get.amount;
slownik。移除(s);
valTemp++;
slownik.put(s,新词(s,valTemp));
}
其他的
{
slownik.put(s,新词(s,1));
}
}
}
}
for(字符串键:slownik.keySet())
{
add(slownik.get(key));
}
Collections.sort(lista,new KomparatorString());
StringBuffer结果=新的StringBuffer();
int关键字计数器=0;
int amountBuff=0;
浮动百分比buff=0;
对于(inti=0;i如果(关键字list.contains(lista.get(i)))是错误的代码


你需要
if(关键字list.contains(lista.get(i.content))

请直接在帖子中发布你的代码,而不是外部链接。你能说明你的目标吗?程序必须计算我的模式,例如我在文本中有多少个“ABDA”。你可以使用Matcher查看[1][1]:我使用ArrayList,但我不知道为什么程序不将我的模式添加到列表中。
public Word(String content, int amount) {
this.content = content;
this.amount = amount;
}

@Override
public String toString() {
return "Word [content=" + content + ", amount=" + amount + "]";
}
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);
}