This code should remove each space from test. Find the error(s) in the following code.

```
1 String test = "s p a c e s";
2 int index;
3
4 while( test.indexOf( " " ) == -1 )
5 {
6 index = test.indexOf( " " );
7 test = test.substring( 0, index - 1 ) + test.substring( index );
8 }
```


The while loop will never execute—it should execute when test.indexOf( " " ) doesn’t return -1, so the == operator on line 4 should be replaced with the != operator. Also, the calls to the substring method on line 7 are incorrect. The first call returns a substring that excludes the character just before the space to be removed, and the second call returns a substring that includes the space you are trying to remove. The arguments should be replaced with index and index + 1, respectively.

```
1 String test = "s p a c e s";
2 int index;
3
4 while( test.indexOf( " " ) != -1 )
5 {
6 index = test.indexOf( " " );
7 test = test.substring( 0, index ) + test.substring( index + 1 );
8 }
```

Computer Science & Information Technology

You might also like to view...

By default, records in a query are displayed in primary key field order, even if that field is NOT included in the query

Indicate whether the statement is true or false

Computer Science & Information Technology

The cin stream input object can always be used in place of getline() for string input.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Which company produces the browser Firefox?

a. Microsoft b. Open Office c. Apple d. Mozilla

Computer Science & Information Technology

The doInOrder and doTogether statements are control structures.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology