Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Implement console program which will meet the following requirements:

    Program starts and asks user to enter random words separated by space

    Program asks user to enter minimum length of string to filter words which were entered

    Program creates array object from entered words

    Program calls specific method which takes String[] as a parameter and returns array of strings which contains words that have length more or equal to value specified by user

    Method should look like this:
    public static String[] filterWordsByLength(int minLength, String[] words) {

             <write your code here>

c. Program prints filtered array to the console output.

guys above is the requirment that asked to me to write the code.. below i attached the code that show me an error which is not satisfy the requirement

Error image

What I have tried:

below i attached the code..

import java.util.*; public class FilterStringArray {

public static void main(String[] args) {
    Scanner sc = new Scanner(;
    System.out.print("Please, enter any words separated by space: ");
    String userInput = sc.nextLine();
    System.out.print("Please, enter minumum word length to filter words: ");
    int minLength = sc.nextInt();
    String[] words = userInput.split("\\s+");
    String[] filteredWords = filterWordsByLength(minLength, words);

public static String[] filterWordsByLength(int minLength, String[] words) {
    String[] filter = new String[words.length];
    for (int i=0; i < words.length ;i++ )
        filter[i] = words[i].substring(0,minLength);
    return filter;
Updated 14-Sep-22 1:50am

WHy are you using .SubString when you should be looking at the .Length of the string?
Share this answer
i used substring for filter the string array. inside the substring i set user input which is given for filter the minimum length
Share this answer
Dave Kreskowiak 14-Sep-22 8:23am    
First, you posted your response as an answer to your own question! Do not do that. Use the "Have a question or comment" button to reply to someone. That why, they get an email that says you posted a repsonse.

Next, you shouldn't be using SubString AT ALL. There is no reason to use it based on the assignment description. All you need to look at is the LENGTH of each string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900