Answer:
import java.util.*;
public class Solution {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the words.");
String userInput = scan.nextLine();
String[] splitedString = userInput.split(" ");
getBigWords(splitedString);
}
public static void getBigWords(String[] listOfWord){
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < listOfWord.length; i++){
if(listOfWord[i].length() > 5){
list.add(listOfWord[i]);
}
}
for (String temp : list) {
System.out.println(temp);
}
}
}
Explanation:
The first line is the import statement which import the classes needed for this class: Scanner and ArrayList.
The class was defined as Solution. Then, the main method is defined. Inside the main method a scanner object is created called scan to receive user input.
The user is prompted for input, which is stored as userInput. The userInput is then splitted and stored in an array called splitedString.
The method that perform the string manipulation is called getBigWords, the method take the splittedString as a parameter.
Next, the getBigWords method is defined; it takes an array as parameter. The parameter is called listOfWord. Inside the getBigWords method, an ArrayList called list is initialized. The list is to hold string whose length is more than 5.
The first for-loop loop through the listOfWord and check if any element has length greater than 5. If there is, the element is added to the list arraylist.
The second for-loop loop through the list to output the words whose length is greater than 5.