java - how I deal with this ArrayIndexOutOfBoundException in this noun finding program -
this question has answer here:
i have came out problem couldn't find way solve exception. please help. program trying find out nouns,verbs , adjectives user given sentence(here tried find out nouns). if had made errors in program please point errors , can correct it. here code:
enter code here import java.io.fileinputstream; import java.io.inputstream; import java.util.hashset; import java.util.set; import opennlp.tools.cmdline.parser.parsertool; import opennlp.tools.parser.parse; import opennlp.tools.parser.parser; import opennlp.tools.parser.parserfactory; import opennlp.tools.parser.parsermodel; import opennlp.tools.util.objectstream; public class parsertest { static set<string> nounphrases = new hashset<>(); static set<string> adjectivephrases = new hashset<>(); static set<string> verbphrases = new hashset<>(); static string result; gui_demo gd = new gui_demo(); string line = practice.gui_demo.textfield1.gettext(); public parsertest(string line) { // todo auto-generated constructor stub } public parsertest() { // todo auto-generated constructor stub } //objectstream<string> linearr = new plaintextbylinestream(new stringreader(line)); string[] linearr = line.split("."); public void getnounphrases(parse p) { if (p.gettype().equals("nn") || p.gettype().equals("nns") || p.gettype().equals("nnp") || p.gettype().equals("nnps")) { nounphrases.add(p.getcoveredtext()); system.out.println(p.getcoveredtext()); } if (p.gettype().equals("jj") || p.gettype().equals("jjr") || p.gettype().equals("jjs")) { adjectivephrases.add(p.getcoveredtext()); system.out.println(p.getcoveredtext()); } if (p.gettype().equals("vb") || p.gettype().equals("vbp") || p.gettype().equals("vbg")|| p.gettype().equals("vbd") || p.gettype().equals("vbn")) { verbphrases.add(p.getcoveredtext()); system.out.println(p.getcoveredtext()); } (parse child : p.getchildren()) { getnounphrases(child); } } public void parseraction() throws exception { try{ inputstream = new fileinputstream("en-parser-chunking.bin"); parsermodel model = new parsermodel(is); parser parser = parserfactory.create(model); for(int i=1; <= linearr.length; i++){ parse topparses[] = parsertool.parseline(linearr[i], parser , 1); //getting exception (parse p : topparses){ //p.show(); getnounphrases(p); } } }catch(exception e){ system.out.print(e); } } public static void main(string[] args) throws exception { new parsertest().parseraction(); result = "nouns :" +nounphrases.toarray(new string[nounphrases.size()]);//+ "\n" + "verbs:" + verbphrases + "adjectives:" + adjectivephrases; /*system.out.println("list of noun parse : "+nounphrases); system.out.println("list of adjective parse : "+adjectivephrases); system.out.println("list of verb parse : "+verbphrases); */ } }
and output of program is:
java.lang.arrayindexoutofboundsexception: 1
you have:
for(int i=1; <= linearr.length; i++) { parse topparses[] = parsertool.parseline(linearr[i], parser , 1); }
the last index of array 1 less length of it, since first index 0
, not 1
. therefore, linearr[linearr.length-1]
last index.
you need change i <= linearr.length
i < linearr.length
.
moreover, i
starts @ 1
, not 0
. if need make sure array has @ least 2 elements, have not. need change well.
final result should be:
for(int i=0; < linearr.length; i++) { parse topparses[] = parsertool.parseline(linearr[i], parser , 1); }
Comments
Post a Comment