Java contains(otherString)给出;“意外类型”;

Java contains(otherString)给出;“意外类型”;,java,string,contains,bluej,Java,String,Contains,Bluej,上述内容将不会编译,并给出错误“意外类型。必需:变量,找到:值” 我不明白为什么String.contains()应该返回布尔值 String doubleSpace = " "; String news = "The cat jumped. The dog did not."; while (news.contains(doubleSpace) = true) { news=news.replaceAll(" ", " "); } while (news.contains(d

上述内容将不会编译,并给出错误“意外类型。必需:变量,找到:值” 我不明白为什么String.contains()应该返回布尔值

String doubleSpace = "  ";
String news = "The cat jumped.  The dog did not.";

while (news.contains(doubleSpace) = true)
{
    news=news.replaceAll("  ", " ");
}
while (news.contains(doubleSpace))
{
    news=news.replaceAll("  ", " ");
}
应该是

while (news.contains(doubleSpace) = true)
while (news.contains(doubleSpace) = true)
=用于分配


==用于检查条件。

您的while循环是错误的,请执行以下操作。您通过使用赋值进行赋值,因此会得到该错误。也无需与true进行比较,因为
包含(…)
函数本身将根据需要返回true或false

while (news.contains(doubleSpace) == true)
{
    news=news.replaceAll("  ", " ");
}

有一个编译错误

应该是

while (news.contains(doubleSpace) = true)
while (news.contains(doubleSpace) = true)
字符串上的方法.contains()已返回布尔值 所以你不应该应用比较

无论如何,如果应用布尔运算符'='而不是'='

所以你的代码可以 while(news.contains(doubleSpace)==true)

或者更准确地说