Removing extra spaces without using a regular expression
Your code is buggy: it fails to copy the last character.

As @Mat'sMug says, defining a function would be a good idea.

The task can be done with one array and one loop.

public static String compressConsecutiveSpaces(String str) {
    if (str.isEmpty()) return str;
    char[] chars = str.toCharArray();
    int dest = 1;
    for (int src = 1; src &lt; chars.length; src++) {
        if (!(chars[src - 1] == ' ' &amp;&amp; chars[src] == ' ')) {
            chars[dest++] = chars[src];
        }
    }
    return new String(chars, 0, dest);
}