Search code examples
javaarraysstringarraylistopennlp

How to pass the ArrayList between methods and classes?


I have this method which returns an arrayList tokens :

public static String[] Tokenize(String input) throws InvalidFormatException, IOException {
    InputStream is = new FileInputStream("en-token.bin");    
    TokenizerModel model = new TokenizerModel(is);   
    Tokenizer tokenizer = new TokenizerME(model);    
    String tokens[] = tokenizer.tokenize(input);     
    for (String a : tokens)
        System.out.println(a);   
    is.close();
    return tokens;
}

I want these tokens to be used in another method:

public static void findName(String[] input) throws IOException {
    InputStream is = new FileInputStream("en-ner-person.bin");   
    TokenNameFinderModel model = new TokenNameFinderModel(is);
    is.close();  
    NameFinderME nameFinder = new NameFinderME(model);   
    Span nameSpans[] = nameFinder.find(input);   
    for(Span s: nameSpans)
        System.out.println(s.toString());           
}

Also, how can I use this tokens arrayList in another class something like,

public class Main{
    public static void main( String[] args) throws Exception
    {
        Anotherclass.Tokenize(input);
        Anotherclass.findName(tokens);

    }
}

I'm unable to figure this out! Please help me.

Thanks a ton!


Solution

  • Just use this:

    Anotherclass.findName(Anotherclass.Tokenize(input));