w3resource
Java Programming Exercies

Java Exercises: Cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters

Java Basic: Exercise-239 with Solution

Internet search engine giant, such as Google accepts web pages around the world and classify them, creating a huge database. The search engines also analyze the search keywords entered by the user and create inquiries for database search. In both cases, complicated processing is carried out in order to realize efficient retrieval, but basics are all cutting out words from sentences.
Write a Java program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.

Input:
English sentences consisting of delimiters and alphanumeric characters are given on one line.
Output: Output a word delimited by one space character on one line.

Sample Solution:

Java Code:

 import java.util.Scanner;

public class Main{
	  public static void main(String[] args) 
       {	
		Scanner sc = new Scanner(System.in);
		System.out.println("Input a sentence (1024 characters. max.)");
		String[] str = ((sc.nextLine()).replace(",", "").replace(".", "")).split(" ");
		int flag = 0;
		System.out.println("\n3 to 6 characters length of words:");
		for(String s: str){
			int l = s.length();
			if(l >= 3 && l <= 6){
				if(flag == 1){
					System.out.print(" ");
				}
					System.out.print(s);
				flag = 1;
			}
		}	
	} 
}

Sample Output:

Input a sentence (1024 characters. max.)
The quick brown fox

3 to 6 characters length of words:
The quick brown fox

Flowchart:

Flowchart: Restore the original string by entering the compressed string with this rule.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to restore the original string by entering the compressed string with this rule.
Next: Write a Java program that compute the maximum value of the sum of the passing integers.

What is the difficulty level of this exercise?