gpt4 book ai didi

java - line.startsWith 不解释字符串

转载 作者:行者123 更新时间:2023-12-02 04:36:39 26 4
gpt4 key购买 nike

我试图不打印以 2 2 等字符串开头的行,但在当前状态下,仅以 notice 开头的行被删除。我对其进行了调试并将输出写入代码行中。我该如何修复它?

感谢任何帮助。

代码:

    int number = Character.getNumericValue(newName.charAt(2));
//here start_zero is `2 2`
String start_zero = new StringBuilder().append(number)
.append(" ").append(number).toString();

try (PrintWriter writer = new PrintWriter(path + File.separator
+ newName);

Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
//here is the first line `2 2`
String line = scanner.nextLine();
//here is start_zero `2 2` too.
if (!line.startsWith("notice") || !line.startsWith(start_zero) ) {

writer.println(line);
writer.flush();
}

}
} catch (Exception e) {
e.printStackTrace();
}

最佳答案

if (!line.startsWith("notice") || !line.startsWith(start_zero) ) {

writer.println(line);
writer.flush();
}

你的问题出在你的 if 语句中。您使用了 OR,但您应该使用 AND

if (!line.startsWith("notice") && !line.startsWith(start_zero) ) {

writer.println(line);
writer.flush();
}

如果第一个返回 true ...这是您的情况,那么第二个在您的代码中不再重要。

关于java - line.startsWith 不解释字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30664740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com