简体   繁体   English

unicode-16 中的 nextLine() 和 while 循环错误行为

[英]nextLine() and while loop misbehavior in unicode-16

Edit: This actually turned into quite an interesting problem.编辑:这实际上变成了一个非常有趣的问题。 After some help from commenters, I posted a self-answer.在评论者的帮助下,我发布了一个自我回答。 I should mention that my project is in Unicode-16, which looks like it was the source of the trouble.我应该提到我的项目是 Unicode-16,这看起来是问题的根源。

The problem is that the loop was not exiting as expected, in what appears to be trivially simple code:问题是循环没有按预期退出,代码看起来很简单:

import java.util.Scanner;

public class Lambda2 {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        while (!input.equals("exit")){
            System.out.println("input is \""+ input + "\"");
            System.out.println(input.equals("exit"));

            input = in.nextLine();
        }
        System.out.println("Thank you!");
    }
}

Run 1:运行 1:

exit
Thank you!

So far, so good.到现在为止还挺好。 But when I enter the loop, I run into trouble:但是当我进入循环时,我遇到了麻烦:

Run 2:运行 2:

asdf
input is "asdf"
false
exit
input is "exit"
false
exit
input is "exit"
false

Last I checked "exit".equals("exit") should return true , not false .最后我检查了"exit".equals("exit")应该返回true ,而不是false I've tried using trim() on my inputs in case there was some skullduggery with new lines... What in the world am I missing??我试过在我的输入上使用trim()以防万一有一些带有新线条的头骨......我到底错过了什么?

Neither of them posted an answer, but with the help of GBlodgett and StephenC , an interesting answer did eventually emerge.两个人都没有贴出的答案,但的帮助下GBlodgettStephenC ,一个有趣的答案也最终会出现。

The problem was that the project is, by necessity, in a UTF encoding, and a BOM character (U-FEFF) was being added to the beginning of the user input, making it 5 characters long.问题是该项目必须采用 UTF 编码,并且在用户输入的开头添加了一个BOM字符 (U-FEFF),使其长度为 5 个字符。

The solution was to remove the BOM character immediately after collection:解决方案是在收集后立即删除 BOM 字符:

input = input.replace("\", "");

What is still somewhat mysterious, however, is why no BOM was added to the first input, but only to the subsequent ones.然而,仍然有些神秘的是,为什么没有将 BOM 添加到第一个输入中,而只添加到后续输入中。 It seems like Run 1 should not have worked.似乎运行 1不应该工作。

I tried this code and it is working fine.我试过这段代码,它工作正常。

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM