我正在創建一個程序,稍後將兩個文件比較在一起。我創建了兩個while循環來檢查用戶輸入的文件是否是正確的文件類型,是否有正確的路徑,並且是正常的文件。第一個while循環檢查第一個輸入文件(稱爲initialFile),第二個while循環檢查第二個輸入文件(稱爲compareFile)。第二個while循環檢查還會檢查以確保comparefile與initialFile不同。兩個用戶輸入是不同的,但程序識別它們相同
我正在運行的問題是當我測試第二個while循環檢查是否捕獲到相同的文件類型輸入時。如果文件類型錯誤,沒有一個有效的文件路徑,或者如果它不是一個普通的文件,但如果我輸入的文件與compareFile相同,initialFile 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循環結束?