Filter string array

Viewed 44
import java.util.*;
public class JavaApplication14 {

 public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    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);
    System.out.println(Arrays.toString(filteredWords));
}

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

}

guys iam following an java course in udemy. above code i wrote for particular question which is ask me in the course. there is an logical error in this code. when i run this code its says its doesn't give an expected output..

below i attached the question requirement and image of error..

click here to view the error image

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.

0 Answers
Related