Filer contents of a string array (Java)

Viewed 63

I am writing a code that takes a string array and filters out certain contents that contain a certain letter. For example, for the array that I have, I want to filter out all the contents that contain the letter "b" and remove them out of the array without creating a new array (removes from the existing array). I am currently using the indexOf method and it is giving me problems. It gives me an error saying that int cannot be converted into string. I am not sure what the probelm is with that.

public class Main {
  public static void main(String[] args) {
    String [] arr = {"abc","bcd", "cad", "aaa"};

    for(int i = 0; i < arr.length; i++){
        for(int j = 0; j < arr[i].length(); j++){
            String index = arr[i].indexOf(j, j + 1);
            if(index == "b"){
                arr[i] = "";
            }
        }
    }

    for(int i = 0; i < arr.length; i++){
        System.out.println(arr[i]);
    }
  }
}
4 Answers

arr[i].indexOf(j, j + 1) should be replaced by arr[i].substring(j, j + 1) because the return type of String#indexOf is an integer, but you're expecting the character back at this position.

and

if(index == "b") by if(index.equals("b")) because you might get unexpected results due to the fact that index and "b" are two different objects. That's the reason why String#equals should be used


That said, there is a more easy way to achieve what you're doing which is more straightforward by using String#contains

public class Main {
  public static void main(String[] args) {
    String [] arr = {"abc","bcd", "cad", "aaa"};

    for(int i = 0; i < arr.length; i++){
        if (arr[i].contains("b")) {
            arr[i] = "";
        }
    }

    for(int i = 0; i < arr.length; i++){
        System.out.println(arr[i]);
    }
  }
}

The issue you're running into is that the String.indexOf method returns the location in the string where another string occurs or -1 if it does not occur. In your case you're looking through each entry in the array and checking to see if j appears in the string.

If you want to use indexOf you could instead use arr[i].indexOf("b"). This would search through the string for the given substring "b". Then you would have to check the returned value to see if it is not equal to -1 and if it isn't then assign that location of the array to an empty string.

Here's an alternate solution using Java's Stream API

import java.util.Arrays;

public class Main {
    public static void main(String... args) {
        String [] arr = {"abc","bcd", "cad", "aaa"};
        arr = Arrays.stream(arr)
                    .filter(str -> !str.contains("b"))  // Remove elements containing "b"
                    .toArray(String[]::new);
        System.out.println(Arrays.toString(arr));
    }
}

By using stream of array indexes:

   IntStream.rangeClosed(0,arr.length-1)
         .forEach(i -> {if (arr[i].contains("b")) arr[i]="";});

or:

 IntStream.rangeClosed(0,arr.length-1)
          .filter(i-> arr[i].contains("b"))
          .forEach(i-> arr[i]="");
Related