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

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

The above will not compile, giving the error "unexpected type. required:variable, found:value" I do not understand why, as String.contains() should return a boolean value.

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
xxyxxyxyx1
  • 79
  • 7

4 Answers4

3
while (news.contains(doubleSpace) = true)

should be

while (news.contains(doubleSpace) == true)

= is for assignment

== is for checking condition.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

your while loop is wrong,do like as below.You are assigning by using assignment value so you are getting that error.And also no need to compare with true because the contains(...) function itself will return true or false as you needed.

while (news.contains(doubleSpace))
{
    news=news.replaceAll("  ", " ");
}
Android Killer
  • 18,174
  • 13
  • 67
  • 90
0

There is a compilation error

while (news.contains(doubleSpace) = true)

should be

while (news.contains(doubleSpace) == true)
Abi
  • 1,335
  • 2
  • 15
  • 28
0

the method .contains() on string already returns boolean so you should not apply comparison

anyways if you do apply apply boolean operator '==' not '='

so your code can be while (news.contains(doubleSpace) == true)

{
    news=news.replaceAll("  ", " ");
}

or more precisely

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