我正在创建一个程序,稍后将比较两个文件。我创建了两个 while 循环,用于检查用户输入的文件是否是正确的文件类型、是否具有有效的路径以及是否是普通文件。第一个 while 循环检查第一个输入文件(称为initialFile),第二个 while 循环检查第二个输入文件(称为compareFile)。第二个 while 循环检查还进行检查以确保 CompareFile 与 initialFile 不同。
我遇到的问题是当我测试第二个 while 循环检查是否捕获相同的文件类型输入时。如果文件类型错误,没有有效的文件路径,或者它不是普通文件,但如果我为compareFile输入与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 循环在应该循环的时候却结束了?
最佳答案
好吧,看看你的代码:
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,看看如何获取文件的绝对路径。
关于java - 两个用户输入不同,但程序将它们识别为相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470662/