1

Given an array. How do I find all of the words in the list that are greater than three characters and add them to a new list

public class RunnerClass {
   public static int counter;
   public static int index=0;
    public static void main(String[]args){
        /*String mainArr[]= new String[5];
        mainArr[0]="Taco";
        mainArr[1]="Pizza";
        mainArr[2]="Hi";
        mainArr[3]="tryy";
        mainArr[4]="live";*/
        String[]myWords = {"Hi", "taco", "g"};
        System.out.println(removeStopWord(myWords));
        System.out.println(counter);
    }

public static String[] removeStopWord(String [] arr){
    counter=0;
    //String [] methodArr = new String[counter];
    //int index=0;

    for(String keep : arr){
        if(3<keep.length()){
            counter++;
        }
    }
    String [] methodArr = new String[counter];
    index=0;


    for (int i = 0; i < arr.length ; i++){
        if (arr[i].length() > 3){
            methodArr[index]=arr[i];
            index++;
        }
    }

    return methodArr;

}

}

Vikas
  • 6,868
  • 4
  • 27
  • 41
JL14
  • 19
  • 2

4 Answers4

1

You can filter the words using java8 stream API

List<String> lst = Stream.of(myWords).filter( s->s.length()>3).collect(Collectors.toList());

Stream.of is an API to create a data stream of array and then use filter API by passing predicate to filter the stream and collect it in collection.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
vibs
  • 11
  • 3
  • Stream.of is API to create a data stream of myWords array and then use filter API by passing predicate to filter the stream and collect it in collection. List lst = Stream.of(myWords).filter( s->s.length()>3).collect(Collectors.toList()); – vibs Apr 20 '19 at 05:53
  • Please add that to your answer so that others who see this in the future will know how your code works. – jro Apr 20 '19 at 05:58
0

You seem like you're on the right track.

You can tidy up a bit using the String#length() method to check whether length is less than 3 or not.

You could also loop just once by adding matching Strings to an ArrayList, which will grow automatically. You can convert the ArrayList back to a String[] just before you return.

This would eliminate the need for the counter. If you want to print the number of words, just print the size of the ArrayList or String[] after it's been returned.

Capn Sparrow
  • 2,030
  • 2
  • 15
  • 32
0

You can use ArrayList and stream APIs,

List<String> list=Arrays.asList("Hi", "taco", "g");
//filer the elements with length > 3 and then collect in a list       
List<String> result=list.stream().filter(ele->(ele.length())>3).collect(Collectors.toList());
Vikas
  • 6,868
  • 4
  • 27
  • 41
0

1. Your code is actually working perfectly fine.

Perhaps why you think your code isn't working is because of the line System.out.println(removeStopWord(myWords));, which is probably returning you something like [Ljava.lang.String;@15db9742. That's just the className + @ + the hex of the hashCode of the array, as noted in this SO post.

Just replace that line with System.out.println(Arrays.toString(removeStopWord(myWords)));, importing java.util.Arrays if necessary.

2. Alternative with ArrayList

Right now, you are looping over the array twice first to count the valid words and then to create a new array with size counter. It's actually an appropriate thing to do, if memory is more of a concern than speed (there's not a lot of speed difference anyway).

If you want something more elegant but has more memory overhead due to dynamic memory allocation:

public static ArrayList<String> removeStopWord(String [] arr){
    ArrayList<String> methodList = new ArrayList<String>();

    for (int i = 0; i < arr.length ; i++){
        if (arr[i].length() > 3){
            methodList.add(arr[i]);
        }
    }


    return methodList;
}

Print the array with System.out.println(Arrays.toString(removeStopWord(mainArr).toArray()));

Samleo
  • 605
  • 5
  • 16