Answer to Question #223346 in Java | JSP | JSF for Bha

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!!


1
Expert's answer
2021-08-04T14:10:50-0400


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();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
05.08.21, 10:13

Dear Bha , if you have serious assignment that requires large amount 

of work and hence cannot be done for free you can submit it as assignment and our 

experts will surely assist you.



Bha
05.08.21, 04:19

Can you explain the logic in it

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS