我希望:
目前我遇到的问题是找到一种方法来调用用于调用文件的两个方法(每个文件一个)到同一个方法,以便进行比较。
两种方法都使用try-catch-while语句,如果我尝试读取while语句之后的所有条目,则只显示一个而不是整个列表。
有没有办法将两个方法的部分作为参数发送到一个新方法?
这是该程序的代码。我知道我在做这个项目的方式存在问题,但我只是按照我的教学方式来做。
File mainEmails = new File("Testrun.txt");
Scanner inputScanner = null;
int counter = 1;
String fullName = null;
String position = null;
String companyName = null;
String telNumber = null;
String emailAddress = null;
try
{
inputScanner = new Scanner(mainEmails);
}
catch(FileNotFoundException e)
{
System.out.println("File has not been found.");
}
while (inputScanner.hasNextLine())
{
String nextLine = inputScanner.nextLine();
String [] splitFile = nextLine.split(",");
for (int i = 0; i <splitFile.length;i++)
{
if(i==0)
{
fullName = splitFile[0];
}
else if(i==1)
{
position = splitFile[1];
}
else if(i==2)
{
companyName = splitFile[2];
}
else if(i==3)
{
telNumber = splitFile[3];
}
else if(i==4)
{
emailAddress = splitFile[4];
}
else if(splitFile[i] == null)
{
System.out.println("You have failed!");
}
}
}
public static void deletionList()
{
File deletionEmails = new File("Testrun1.txt");
Scanner inputScanner1 = null;
String deletionfullName = null;
String deletionposition = null;
String deletioncompanyName= null;
String deletiontelNumber = null;
String deletionemailAddress = null;
try
{
inputScanner1 = new Scanner(deletionEmails);
}
catch(FileNotFoundException e)
{
System.out.println("File has not been found.");
}
while (inputScanner1.hasNextLine())
{
String deletionnextLine = inputScanner1.nextLine();
String [] deletionsplitFile = deletionnextLine.split(",");
for (int i = 0; i <deletionsplitFile.length;i++)
{
if(i==0)
{
deletionfullName = deletionsplitFile[0];
}
else if(i==1)
{
deletionposition = deletionsplitFile[1];
}
else if(i==2)
{
deletioncompanyName = deletionsplitFile[2];
}
else if(i==3)
{
deletiontelNumber = deletionsplitFile[3];
}
else if(i==4)
{
deletionemailAddress = deletionsplitFile[4];
}
else if(deletionsplitFile[i] == null)
{
System.out.println("You have failed!");
}
}
}
}
我要做的是从第一个拆分中获取fullName,emailAddress,从第二个拆分中获取deletedfullName和deletionemailAddress,并分别比较每个拆分的第一个和第二个。每个文件中都有许多字段,我只对fullName和emailAddress字段感兴趣。
答案 0 :(得分:0)
除了你的问题不是很清楚之外,你至少还有一个明显的问题:
不要对逻辑使用异常处理!异常处理应仅适用于例外。
其次,想想你真正想做的事情。在伪代码中,它看起来像这样:
list1 = split(file(name1).read())
list2 = split(file(name2).read())
list3 = unique(list1, list2)
您的代码是什么样的?
答案 1 :(得分:0)
了解您如何尝试实施解决方案非常令人困惑,因此我建议您查看执行整个读取和比较过程的不同方法。例如,我建议做这样的事情......(在伪代码中)
public void compareFiles(String file1, String file2){
// Read the lines of each file into String[] arrays
String[] file1Lines = readAndSplitIntoLines(file1);
String[] file2Lines = readAndSplitIntoLines(file2);
// compare the lines
for (int x=0;x<file1Lines.length;x++){
for (int y=0;y<file2Lines.length;y++){
if (file1Lines[x].equals(file2Lines[y])){
// match. set it to null
file1Lines[x] = null;
file2Lines[y] = null;
// break out of the inner loop and start comparing the next line
break;
}
}
// remove the duplicates (which are now null values), creating a smaller array of uniques.
String[] newFile1 = shrinkArrayByRemovingNulls(file1Lines);
String[] newFile2 = shrinkArrayByRemovingNulls(file2Lines);
}