我正在创建一个程序,稍后将两个文件进行比较。我创建了两个while循环,用于检查用户输入的文件是否是正确的文件类型,有一个有效的途径,并且是普通文件。第一个while循环检查第一个输入文件(称为initialFile),第二个while循环检查第二个输入文件(称为compareFile)。第二个while循环检查还会检查以确保comparefile与initialFile不同。
我正在运行的问题是当我测试第二个while循环检查是否捕获相同的文件类型输入时。如果文件类型错误,没有有效的文件路径,或者文件不是普通文件,但是如果我输入的文件与initialFile相同,那么第二个while循环正确不会结束while循环结束什么时候应该循环。
以下是相关代码:
import java.util.Scanner;
import java.io.File;
import java.lang.Exception;
import java.io.FileNotFoundException;
public class QuickSortAnagram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String initialInput = " ";
String compareInput = " ";
//Check to make sure FIRST user input is the correct file type, is a valid file pathway, and is a normal file
/*while loop requires user to input a String that ends with ".txt" before
*the loop will end. This ensures that the user will input a String
*that represents the correct file type before being allowed to move on.
The while loop also requires that the user input a filename that has
a valid pathway to it and is a normal file or the user will not be allowed to move on*/
boolean initialWhileLoopEnd = false;
while(initialWhileLoopEnd == false) {
System.out.println("Enter initial test file (Please make sure it is in the form 'textfile.txt', that there is a valid pathway to file, and that file is normal or program will not be able to continue): ");
initialInput = scnr.next();
File initialFile = new File(initialInput);
if(initialInput.endsWith(".txt")) {
if(initialFile.isFile()) {
initialWhileLoopEnd = true;
}
else {
System.out.println("File does not exist and/or is not a normal file");
}
}
else {
System.out.println("Invalid file type");
}
}
//initializes file outside of while loop so it can be read later
File initialFile = new File(initialInput);
//Check to make sure SECOND user input is the correct file type, is a valid file pathway, is a normal file, and is not the same file name that user used for the FIRST check
/*while loop requires user to input a String that ends with ".txt" before
*the loop will end. This ensures that the user will input a String
*that represents the correct file type before being allowed to move on.
The while loop also requires that the user input a filename that has
a valid pathway to it and is a normal file or the user will not be allowed to move on*/
boolean compareWhileLoopEnd = false;
while(compareWhileLoopEnd == false) {
System.out.println("Enter compare test file (Please make sure it is in the form 'textfile.txt', that there is a valid pathway to file, that file is normal, and that file is not identical to the initial file inputed or program will not be able to continue): ");
compareInput = scnr.next();
File compareFile = new File(compareInput);
if(compareInput.endsWith(".txt")) {
if(compareFile.isFile()) {
if(compareFile != initialFile) {
compareWhileLoopEnd = true;
}
else {
System.out.println("File is identical to the initial file inputed. Please input a different file");
}
}
else {
System.out.println("File does not exist and/or is not a normal file");
}
}
else {
System.out.println("Invalid file type");
}
}
File compareFile = new File(compareInput);
scnr.close();
}
}
为什么我的while循环应该循环结束?
答案 0 :(得分:2)
好吧,看看你的代码:
if (compareFile != initialFile) {
compareWhileLoopEnd = true;
}
因此,如果第二个File对象与第一个File对象不同(总是大小写),则结束循环。
应该是
if (compareFile.equals(initialFile)) {
System.out.println("File is identical to the initial file inputed. Please input a different file"); }
else {
compareWhileLoopEnd = true;
}
请注意,即使使用上述代码,它也不是绝对正确的,因为第一个文件可能是foo.txt
而第二个文件可能是../currentDirectory/foo.txt
,这不会是平等的虽然它们实际上都引用了文件系统上的相同文件。如果你想了解这些问题,请查看File的javadoc,看看如何获取文件的绝对路径。