在java中识别三个文本文件中的重复值

plicqrtu  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(311)

您的程序需要搜索这三个文件,以找到在所有三个商店使用的信用卡号码。然后输出这个数字。
您可以遵循下面的粗略伪代码。请注意,它是不完整的,您可能需要解释丢失的部分。正确地记录和注解代码。
main方法:处理first store声明并初始化一个布尔变量match,当match为false时从文件中读取信用卡号,并且信用卡号不为null,将信用卡号传递给处理第二个存储的方法。如果返回true,则将match设置为true,否则从(2)重复。如果match为true或信用卡号为null,则完成程序。根据match的值,输出匹配结果或指示未找到匹配项。
这就是问题所在,我已经编写了代码,但问题是它在另一个文本文件中逐行比较,而不是只取一个值,然后将它与另一个文件中的整个值进行比较。请帮助,一个小的想法也将不胜感激。请在概念上帮助我!

public static void main(String[] args) throws IOException {
        Boolean match = false;
        File file = new File("/Users/User/Desktop/CreditCards/creditCards1.txt");
        Scanner scan = new Scanner(file);
        String credit_card_number = "";
        while (scan.hasNextLine() && !match ) {
             credit_card_number = scan.nextLine();
             match = second_Store(credit_card_number);
            System.out.println(credit_card_number);

        } scan.close();
        if (match == true){
            ;
        }else
            System.out.println(credit_card_number);
        System.out.println(match);

    }

    public static Boolean second_Store(String creditCard) throws FileNotFoundException {
        Boolean match = false;
        File file = new File("/Users/User/Desktop/CreditCards/creditCards2.txt");
        Scanner scan = new Scanner(file);
        String credit_card_number;String toCompare = creditCard;

        do{
            credit_card_number = scan.nextLine();
            if (credit_card_number.equals(creditCard)){
                third_Store(credit_card_number);
                System.out.println(credit_card_number);
                match = true;
            }else
                continue;

        }while(match && scan.hasNextLine());
        return match;

    }

    public static Boolean third_Store(String creditCard) throws FileNotFoundException {
        Boolean match = false;
        File file = new File("/Users/User/Desktop/CreditCards/creditCards3.txt");
        Scanner scan = new Scanner(file);
        String credit_card_number;
        do{
            credit_card_number = scan.nextLine();
            if(credit_card_number.equals(creditCard)){
                match = true;
                System.out.println(credit_card_number);
            }
            else
                continue;

            } while (match && scan.hasNextLine());
        return match;
    }
}
jgwigjjp

jgwigjjp1#

如果您已经知道集合:
将每个存储数据读入一个集合,该集合将包含该存储的所有唯一编号
迭代集合2并删除集合1中不包含的所有值
迭代集合3并删除集合2中未包含的所有数字
集合3应该只包含在所有3个存储中找到的数字

inb24sb2

inb24sb22#

下面是一个简单的程序,用于在3个文件(当前为“path1”、“path2”和“path3”)中查找一行,如果您放置了正确的包,则可以编译这些文件:

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.nio.file.Files;
import java.io.File;
import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        System.out.println("Common line: " + getCommonLine(new File("path1"), new File("path2"), new File("path3")));
    }

    /**Gets a common line in the specified files */
    private static String getCommonLine(/* Any amount of file arguments */ File... files) throws IOException {
        /* A list of the file's lines */
        List<Set<String>> fileLines = new ArrayList<>();
        /* The combined file's lines */
        Set<String> allLines = new HashSet<>();

        /* Get the lines in all files */
        for(File file : files) {
            List<String> lines = Files.readAllLines(file.toPath());
            fileLines.add(new HashSet<String>(lines));
            allLines.addAll(lines);
        }

        /* Loop through the combined lines */
        for(String line : allLines) {
            /* Check if all files contain the line */
            boolean result = true;
            for(Set<String> otherFileLines : fileLines) {
                if(!otherFileLines.contains(line)) {
                    result = false;
                }
            }
            if(result) {
                return line;
            }
        }
        return null;
    }

}

我没有时间运行它,所以它可能不工作。

相关问题