KnowledgeBoat Logo

Computer Applications

Write a Java program that reads a line of text separated by any number of whitespaces and outputs the line with correct spacing, that is, the output has no space before the first word and exactly one space between each pair of adjacent words.

Java

Input in Java

21 Likes

Answer

import java.util.Scanner;

public class KboatCorrectSpacing
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence: ");
        while(in.hasNext()) {
            String w = in.next();
            System.out.print(w + " ");
        }
        in.close();
    }
}

Output

BlueJ output of Write a Java program that reads a line of text separated by any number of whitespaces and outputs the line with correct spacing, that is, the output has no space before the first word and exactly one space between each pair of adjacent words.

Answered By

6 Likes


Related Questions