Create a function that takes an array of strings and returns an array with only the strings that have numbers in them. If there are no strings containing numbers, return an empty array.
Examples numInStr(["1a", "a", "2b", "b"]) ➞ ["1a", "2b"]
numInStr(["abc", "abc10"]) ➞ ["abc10"]
numInStr(["abc", "ab10c", "a10bc", "bcd"]) ➞ ["ab10c", "a10bc"]
numInStr(["this is a test", "test1"]) ➞ ["test1"]
This is my code
import java.util.ArrayList;
public class Challenge {
public static String[] numInStr(String[] arr) {
boolean addWord = false;
ArrayList<String> output = new ArrayList<String>();
for (String word: arr){
for (int i =0; i<word.length(); i++){
if (Character.isDigit(word.charAt(i))){
addWord = true;
}
}
if (addWord) {
output.add(word);
addWord=false;
}
return output;
}
}
}
What is wrong?
Also why do I have to set a type for arraylist? Whenever I do, it says its unsafe. How would I do an arraylist that contains multiple datatypes?