I need to write a code that checks all the words in an array list and tells me how many of these have a certain length. I understand how to do that, but I can't figure out how to make the ArrayList to be read in the second class so I can apply it in the program.
MAIN
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
java.util.ArrayList myList = new java.util.ArrayList();
myList.add("cat");
myList.add("mouse");
myList.add("frog");
myList.add("dog");
myList.add("dog");
int len = in.nextInt();
WordList wl = new WordList();
wl.numWordsOfLength(len);
}
}
SECOND CLASS
public class WordList{
public int numWordsOfLength(int len){
int count = 0;
for(int i=0;i<len;i++){
if(((String)myList.get(i)).length()==len){
count++;
}
}
return count;
}
}