String.equals() 方法在 Windows 中不匹配

String.equals() method not matching in Windows

我在 macOS 上使用 Eclipse 创建了一个 Java 程序,它 运行 很好。当我在 Windows 将它切换到 Eclipse 时,我开始收到错误消息。

失败条件是在 mac 上运行良好的 if 语句。在 Windows 我尝试调试问题。如果我打印出来,scanner.next() 给出了正确的输出。如果我比较它但是它没有用。我也尝试将输出复制并粘贴到我的条件中,但它仍然不起作用。我尝试包括是否有一个我看不到的 \n,这也没有用。下面是它的代码。

Scanner scanner = new Scanner("text from file with line 1 being 'TRAILING PERFORMANCE'");
toggleDelimiter(scanner); // helper method that switches the scanner to read by lines or tokens

if(!scanner.next().equals("TRAILING PERFORMANCE")) {
// Throw error here if the first line is not TRAILING PERFORMANCE
}

条件应该 return false 并且我正在使用的文件不会抛出错误,因为我知道它可以工作。如前所述,完全相同的代码在 macOS.

的 Eclipse 上运行良好

编辑: 我现在也尝试将 \n、\n\r 和 System.lineSeparator() 附加到 "TRAILING PERFORMACE" 的末尾,但它仍然不起作用。

在了解到 Windows 无需我做任何事情就可以更改换行符之后,我能够在 PDFBox(我用来阅读 PDF)中找到方法 setLineSeparator() 并设置新的行字符为“\n”,这样它就可以像以前一样工作了。