gpt4 book ai didi

java - 三个文件中的匹配编号未显示

转载 作者:行者123 更新时间:2023-12-02 02:18:06 24 4
gpt4 key购买 nike

我正在编写的程序允许用户在三个文本文件中查找匹配的信用卡号。但是,它输出在文件之间的任何比较中都没有找到匹配项。如果有人可以指导我如何解决这个问题,那就太好了!下面是该程序的代码。

编辑:我似乎忘记在文件之间的一些比较中放置数字 1000。我现在在
遇到空异常问题 if(numbers1[i].compareTo(numbers2[i]) == 0){

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MatchingNumber {
public static int counter = 0;
public static int flag;
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
static BufferedReader in3 = new BufferedReader(new InputStreamReader(System.in));
static int x;
static String[] numbers1 = new String[1000];
static String[] numbers2 = new String[1000];
static String[] numbers3 = new String[1000];

public static void main(String[] args) throws IOException {
loadNumbers();
firstCompare();
secondCompare();
thirdCompare();
}
public static void loadNumbers() throws IOException {

String findFile, file;
//ask for location of file
System.out.println("Enter File 1 Location: ");
//Read input
findFile = in.readLine();
//find file
file = findFile + "/creditCards1.txt";
BufferedReader in = new BufferedReader(new FileReader(file));

String findFile1, file1;
//ask for location of file
System.out.println("Enter File 2 Location: ");
//Read input
findFile1 = in2.readLine();
//find file
file1 = findFile1 + "/creditCards2.txt";
BufferedReader in2 = new BufferedReader(new FileReader(file1));

String findFile2,file2;
//ask for location of file
System.out.println("Enter File 3 Location: ");
//Read input
findFile2 = in3.readLine();
//find file
file2 = findFile2 + "/creditCards3.txt";
BufferedReader in3 = new BufferedReader(new FileReader(file2));


for (int i = 0; i < 1000; i++){
//read in the data
numbers1[i] = in.readLine();
numbers2[i] = in2.readLine();
numbers3[i] = in3.readLine();
counter++;
}
in.close();
in2.close();
in3.close();
}

public static void firstCompare() {
boolean found = false;
for (int i = 0; i < 1000; i++){
if(numbers1[i].compareTo(numbers2[i]) == 0){
flag = i;
found = true;
System.out.println(flag + "is the matching number in files 1 and 2");
}
}
if (!found){
System.out.println("No matches found files 1 and 2");

}
}

public static void secondCompare() {
boolean found = false;
for (int i = 0; i < 1000; i++){
if(numbers1[i].compareTo(numbers3[i]) == 0){
flag = i;
found = true;
System.out.println(flag + "is the matching number in files 1 and 3");
}
}
if (!found){
System.out.println("No matches found files 1 and 3");
}
}

public static void thirdCompare() {
boolean found = false;
for (int i = 0; i < 1000; i++){
if(numbers2[i].compareTo(numbers3[i]) == 0){
flag = i;
found = true;
System.out.println(flag + "is the matching number in files 2 and 3");
}
}
if (!found){
System.out.println("No matches found files 2 and 3");
}

}

}

最佳答案

首先,为什么要限制信用卡号码的数量?

static String[] numbers1 = new String[1000];  
static String[] numbers2 = new String[1000];
static String[] numbers3 = new String[1000];

我建议使用:

static List<String> numbers1 = new List<String>();
static List<String> numbers2 = new List<String>();
static List<String> numbers3 = new List<String>();

这将使您能够提高项目的可扩展性并减少比较时的冗余。

比较时,您可以简单地循环遍历一个列表并检查另一个列表是否包含您要查找的元素:

boolean found = false;
int i = 0;
while(!found)
{
if(numbers1.Contains(numbers2[i])
found = true;
i++;
}

关于java - 三个文件中的匹配编号未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57289982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com