获得三重复制时的错误值

时间:2015-10-07 06:02:17

标签: java

我开发了一个java代码,它将文本文件作为输入并选择重复的单词并通过创建包含重复单词的新文本文件来提供输出,现在我需要它来选择三重复的单词,但我无法得到它正确。下面是我的java代码 -

import java.util.*;
import java.io.*;
public class CheckDuplicate {


public static void main(String[] args) throws Exception{

    // TODO Auto-generated method stub

    FileReader file1=new FileReader("/home/goutam/workspace/DuplicateWord/clean_2014.txt");

    BufferedReader reader1=new BufferedReader(file1);

    File f=new File("Reduplication.txt");

    FileWriter fw=new FileWriter(f);

    String line=reader1.readLine();

    while(line!=null){

        String[] arr=line.split(" ");

        if(arr.length>1){

            for(int i=0;i<arr.length;i++){

                if(i<arr.length-1){

                    int cmp=arr[i].compareTo(arr[i+1]);

                    if(cmp==0){

                        fw.write(arr[i].toString());

                        fw.write("\n");

                    }

                }

            }
        }

        line=reader1.readLine();

    }
    reader1.close();

    file1.close();
}

}

5 个答案:

答案 0 :(得分:4)

您的代码不起作用,因为您只考虑相邻的元素。

不是使用嵌套循环,而是使用Map将String作为值和一个指示计数为值的整数,轻松实现您想要的目标。

  • 当您第一次遇到字符串时,将其插入值为1
  • 如果地图中已有字符串,则只需增加其值
  • 即可

然后你可以迭代值并选择值为&gt;的键。你想要什么。

我强烈建议您使用调试器,它可以帮助您更好地理解程序的流程。

答案 1 :(得分:0)

由于您希望项目连续出现3次,因此我修改了代码以实现您的目标:

public static void main(String[] args) throws Exception {

    FileReader file1 = new FileReader("/home/goutam/workspace/DuplicateWord/clean_2014.txt");

    BufferedReader reader1 = new BufferedReader(file1);

    File f = new File("Reduplication.txt");

    FileWriter fw = new FileWriter(f);

    String line = reader1.readLine();

    while (line != null) {

        String[] arr = line.split(" ");

        if (arr.length > 1) {

            for (int i = 0; i < arr.length; i++) {

                if (i < arr.length - 2) {   // change from length-1 to length-2

                    int cmp = arr[i].compareTo(arr[i + 1]);

                    if (cmp == 0) {
                        if (arr[i + 1].equals(arr[i + 2])) { // keep comparing the next 2 items

                            System.out.println(arr[i].toString() + "\n");

                            fw.write(arr[i].toString());
                            fw.write("\n");
                        }
                    }
                }
            }
        }

        line = reader1.readLine();

    }
    reader1.close();

    file1.close();
}

答案 2 :(得分:0)

这应该做的工作,请注意:我没有编译也没有测试它,但至少它应该为你提供一些指示。

public void findRepeatingWords( int atLeastNRepetitions ) {
    try ( BufferedReader reader1 = new BufferedReader( new FileReader("/home/goutam/workspace/DuplicateWord/clean_2014.txt") ) ) {
        // There are libraries that can do this, but yeah... doing it old style here
        // Note that usage of AtomicInteger is just a convenience so that we can reduce some lines of codes, not used for atomic operations
        Map<String, AtomicInteger> m = new LinkedHashMap<String, AtomicInteger>() {
            @Override
            public AtomicInteger get( Object key ) {
                AtomicInteger cnt = super.get( key );
                if ( cnt == null ) {
                    cnt = new AtomicInteger( 0 );
                    super.put( key, cnt );
                }
                return cnt;
            }
        };

        String line = reader1.readLine();
        while( line!=null ){
            // Note we use \\W here that means non-word character (e.g. spaces, tabs, punctuation,...)
            String[] arr = line.split( "\\W" );
            for ( String word : arr ) {
                m.get( word ).incrementAndGet();
            }
            line = reader1.readLine();
        }
    }
}    

private void writeRepeatedWords( int atLeastNRepetitions, Map<String, AtomicInteger> m ) {
    File f = new File( "Reduplication.txt" );
    try ( PrintWriter pw = new PrintWriter( new FileWriter( f ) ) ) {
        for ( Map.Entry<String, AtomicInteger> entry : m.entrySet() ) {
            if ( entry.getValue().get() >= atLeastNRepetitions ) {
                pw.println( entry.getKey() );
            }
        }
    }
}

答案 3 :(得分:0)

以下是您要搜索的内容,我使用LinkedHashMap执行了它,它是一个动态代码,您不仅可以选择double,triple,还可以选择n次。

   sc = someclass_type();

答案 4 :(得分:0)

尝试此代码打印如果count大于3,则可以使用任何数字

  'FrontController' => 
  array (
    'path' => 'override/classes/controller/FrontController.php',
    'type' => 'class',
    'override' => false,
  ),