Question #223346

Write a program that replaces two or more consecutive blanks in a string by a

single

blank. For example, if the input is

Grim return to the planet of apes !!

the output should be

Grim return to the planet of apes!!


Expert's answer



import java.util.Scanner;
public class ReplaceBlanks {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); 
        String text=input.nextLine();     
        System.out.println(new String(text).trim().replaceAll("\\s{2,}", " "));
        input.close();
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS