So I have a simple method that takes two inputs a String and an array list of strings. I try to search for the given string in the given array list and if it’s found I return it, if not I return a statement saying that it wasn’t found
here’s my code:
String findWord(String word, ArrayList<String> list){
for(int i=0; i<list.size(); i++)
if(word.equals(list.get(i)))
return word;
else
return "Word not found";
}
I also tried:
String findWord(String word, ArrayList<String> list){
String myString = null;
for(int i=0; i<list.size(); i++)
if(word.equals(list.get(i)))
myString= word;
else
myString = "Word not found";
return myString;
}
but the return value is always null.
Try,