【问题标题】:Deduplication from text file using scanner without arrays (Java)使用没有数组的扫描仪从文本文件中删除重复数据 (Java)
【发布时间】:2019-09-24 19:49:49
【问题描述】:

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

目前我已经尝试从第一个文本文件中读取数字,并使用一个 while 循环来检查下一个数字是否相似,使用扫描仪。唯一的问题是循环也会获取下一个整数,因此该算法仅在所有数字都重复时才有效。如果有人至少能指出我正确的方向,这会让我很开心,在此先感谢!

示例文本文件一(所有整数都在新行上):5 5 5 5 5 8 8 9 9 9 9 10 10 11

我的输出:5 8 9 10 预期输出:5 8 9 10 11

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();
}

【问题讨论】:

    标签: java arrays text


    【解决方案1】:

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

    【讨论】:

    • 谢谢,但很遗憾,我们不能使用集合或任何其他数据保存结构(教授希望这样)。
    • 由于整数已排序,您可以将第一行读取为 int previousInteger 并将其设置为循环外的 nextInteger。然后在循环中读取 nextInteger 中的整数并将其与 previousInteger 进行比较。如果两者不同,则将其写入输出文件并设置 previousInteger=nextInteger 并继续。
    • 好吧,这听起来很合乎逻辑,我会试一试。谢谢!
    【解决方案2】:

    我修改了代码的第二部分,将数字之间的比较添加到变量“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();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多