从 java 中的三个文本文件中识别重复值
Identify duplicate values from three text files in java
您的程序需要搜索这三个文件以找到在所有三个商店使用的信用卡号。然后输出那个数字是多少。
您可以按照下面粗略的伪代码进行操作。请注意,它是不完整的,您可能需要解释缺失的部分。正确记录和注释您的代码。
主要方法:处理第一个商店
声明并初始化一个布尔变量 match 为 false
从文件中读取信用卡号
while match 为 false,且信用卡号不为空
将信用卡号传递给处理第二家商店的方法。如果它 returns true 将匹配设置为 true,否则从 (2) 开始重复
如果匹配为真或信用卡号为空,则结束该程序。根据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;
}
}
如果您已经了解集合:
- 将每个商店数据读入一个 Set,然后它将包含该商店的所有唯一编号
- 迭代集合 2 并删除集合 1 中不包含的所有值
- 迭代集合 3 并删除集合 2 中不包含的所有数字
- 第 3 组应该只包含在所有 3 家商店中找到的号码
这是一个简单的程序,用于在 3 个文件(当前为“路径 1”、“路径 2”和“路径 3”)中找到一行,如果您放入正确的包,该行将编译:
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;
}
}
虽然我没有时间 运行 它可能行不通。
您的程序需要搜索这三个文件以找到在所有三个商店使用的信用卡号。然后输出那个数字是多少。
您可以按照下面粗略的伪代码进行操作。请注意,它是不完整的,您可能需要解释缺失的部分。正确记录和注释您的代码。
主要方法:处理第一个商店 声明并初始化一个布尔变量 match 为 false 从文件中读取信用卡号 while match 为 false,且信用卡号不为空 将信用卡号传递给处理第二家商店的方法。如果它 returns true 将匹配设置为 true,否则从 (2) 开始重复 如果匹配为真或信用卡号为空,则结束该程序。根据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;
}
}
如果您已经了解集合:
- 将每个商店数据读入一个 Set,然后它将包含该商店的所有唯一编号
- 迭代集合 2 并删除集合 1 中不包含的所有值
- 迭代集合 3 并删除集合 2 中不包含的所有数字
- 第 3 组应该只包含在所有 3 家商店中找到的号码
这是一个简单的程序,用于在 3 个文件(当前为“路径 1”、“路径 2”和“路径 3”)中找到一行,如果您放入正确的包,该行将编译:
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;
}
}
虽然我没有时间 运行 它可能行不通。