Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不带阵列的扫描仪从文本文件中删除重复数据(Java)_Java_Arrays_Text - Fatal编程技术网

使用不带阵列的扫描仪从文本文件中删除重复数据(Java)

使用不带阵列的扫描仪从文本文件中删除重复数据(Java),java,arrays,text,Java,Arrays,Text,我试图读取一个文本文件,其中包含不同行上的整数,这些整数已经从最小到最大排序。所述整数必须传输到另一个文本文件中,但不得有任何副本,也不得使用任何类型的数组、数组列表、映射、集合或任何其他类型的数据结构 目前,我已经尝试从第一个文本文件中读取数字,并使用while循环检查下一个数字是否相似。唯一的问题是循环也会得到下一个整数,因此该算法仅在所有数字都重复的情况下才有效。如果有人至少能给我指出正确的方向,这会让我高兴的,提前谢谢 示例文本文件1所有整数都位于新行:5 5 5 8 9 9 9 10

我试图读取一个文本文件,其中包含不同行上的整数,这些整数已经从最小到最大排序。所述整数必须传输到另一个文本文件中,但不得有任何副本,也不得使用任何类型的数组、数组列表、映射、集合或任何其他类型的数据结构

目前,我已经尝试从第一个文本文件中读取数字,并使用while循环检查下一个数字是否相似。唯一的问题是循环也会得到下一个整数,因此该算法仅在所有数字都重复的情况下才有效。如果有人至少能给我指出正确的方向,这会让我高兴的,提前谢谢

示例文本文件1所有整数都位于新行:5 5 5 8 9 9 9 10 11

我的产出:589910 预期产出:58991011

public static void deduplicateFiles(String inputFileName,String outputFileName){
    Scanner scan = null;
    try{
        scan = new Scanner(new FileInputStream(inputFileName) );
    }catch(FileNotFoundException e){
        System.out.println(e.getMessage() );
    }
    PrintWriter writer = null;
    try{
        writer = new PrintWriter(outputFileName);
    }catch(FileNotFoundException e){
        System.out.println(e.getMessage());
    }
    while(true){ 
        int firstInt = scan.nextInt();
       scan.nextLine();
       //if(scan.nextInt() != firstInt)
        int counter = 1;

        while(scan.nextInt() == firstInt && scan.hasNext() != false){
                System.out.println("counter"  +counter);
                counter++;
                scan.nextLine();
        }
        System.out.println("The integers:" + firstInt);
        writer.println(firstInt);
        if(scan.hasNext() == false)
            break;
    }    
    writer.flush();
    writer.close();
}

首先读取文件中的所有整数。将它们添加到集合,然后从集合写入文件。您可以使用LinkedHashSet来维持订单。

我修改了代码的第二部分,将数字之间的比较添加到变量“nextInt”中。这主要是关于正确位置的条目,我希望它能解决您的问题:

public void deduplicateFiles (String inputFileName, String outputFileName){
    Scanner scan = null;
    try {
        scan = new Scanner(new FileInputStream(inputFileName));
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(outputFileName);
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
    while (true) {
        //this two lines are initial
        int firstInt = scan.nextInt();
        int counter = 1;

        //next lines are compare adjacent values
        while (scan.hasNextLine()) {
            int nextInt = scan.nextInt();
             if (nextInt == firstInt) {
                counter++;
            } else {
                System.out.println("counter " + counter);
                counter = 1;
                System.out.println(firstInt);
                 writer.println(firstInt);
                 firstInt = nextInt;
            }
        }

        //this three lines terminate adding
        writer.print(firstInt);
        System.out.println("counter " + counter);
        System.out.println(firstInt);

        break;

    }
    writer.flush();
    writer.close();
}

谢谢,但不幸的是,我们不能使用集合或任何其他数据存储结构,因为教授希望这样做。由于整数已排序,您可以将第一行读取为int previousInteger,并将其设置为循环外的nextingteger。然后在循环内部读取nextInteger中的一个整数,并将其与前一个整数进行比较。如果两者不同,则将其写入输出文件,并设置previousIneteger=nextingteger,然后继续。好的,这听起来很符合逻辑,我会尝试一下。非常感谢。