Ich erstelle ein Programm, das später zwei Dateien miteinander vergleichen wird. Ich habe zwei While-Schleifen erstellt, die prüfen, ob vom Benutzer eingegebene Dateien den richtigen Dateityp haben, einen gültigen Pfad zu ihnen haben und normale Dateien sind. Die erste while-Schleife prüft die erste eingegebene Datei (namens initialFile) und die zweite while-Schleife prüft die zweite eingegebene Datei (compareFile genannt). Die zweite while-Schleife überprüft auch, um sicherzustellen, dass comparefile sich von der initialFile unterscheidet.Zwei Benutzereingaben sind unterschiedlich, aber das Programm erkennt sie als das gleiche
Das Problem, zu dem ich laufe, ist, wenn ich getestet habe, ob die zweite while-Schleife eine identische Dateityp-Eingabe abfängt. Die zweite while-Schleife wird nicht korrekt beendet, wenn der Dateityp falsch ist, es keinen gültigen Pfad zur Datei gibt oder wenn es keine normale Datei ist, aber wenn ich eine Datei für compareFile eingabe, die identisch mit initialFile ist, endet die while-Schleife wenn es Schleife sollte.
Hier ist der relevante Code:
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();
}
}
Warum while-Schleife endet, wenn es Looping werden sollte?