java - how to find i of a token in an array[i] -
so, i've found word in document , print line in word present this:
say example file contains : "the quick brown fox jumps on lazy dog.jackdaws love big sphinx of quartz."
fileinputstream fstream = new fileinputstream(file); bufferedreader br = new bufferedreader(new inputstreamreader(fstream)); string strline; //read file line line while((strline = br.readline()) != null){ //check see whether testword occurs @ least once in line of text check = strline.tolowercase().contains(testword.tolowercase()); if(check){ //get line, , parse words string array string[] linewords = strline.split("\\s+"); for(int i=0;i<linewords.length;i++){ system.out.print(linewords[i]+ ' '); }
and if search 'fox' , linewords[] contain tokens first sentence. , linewords[3] = fox. print color of fox, need linewords[2]. wondering how can 'i' of token in linewords[i], because want output linewords[i-1]
that can done traversing array , when word , print 1 before it.here's how:-
if(linewords[0].equals(testword) return;//no preceding word for(int i=1;i<linewords.length;i++){ if(linewords[i].equals(testword){ system.out.println(linewords[i-1]); break; } }
Comments
Post a Comment