两个用户输入不同但程序将它们识别为相同

Two User Inputs Are Different but Program Recognizes Them as the Same

我正在创建一个稍后将两个文件一起比较的程序。我创建了两个 while 循环来检查用户输入的文件是否是正确的文件类型、是否具有通往它们的有效路径以及是否是普通文件。第一个 while 循环检查第一个输入文件(称为 initialFile),第二个 while 循环检查第二个输入文件(称为 compareFile)。第二个 while 循环检查还检查以确保 comparefile 与 initialFile 不同。

我 运行 遇到的问题是当我测试第二个 while 循环检查是否捕获到相同的文件类型输入时。如果文件类型错误,则第二个 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 循环在应该循环的时候结束了?

好吧,看看你的代码:

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,看看如何获​​取文件的绝对路径。