Find word using Java -


i trying write java class find word surrounded ( ) in text file , output word , occurrences in different line.

how can write in java?

input file

school (aaa) (aaa) 10/22/2011 ssss(ffs) (ffs) 7368 house 8/22/2011(h76yu)  come 789  (aaa) car (h76yu)  (h76yu) extract9998790 2/3/2015 (aaa)  

output file

(aaa) 4     (ffs) 2     (h76yu) 3  

this got far..

public class  findtextoccurances  { public static void main(string[] args) throws ioexception {      int sum=0     string line = value.tostring();      (string word : line.split("(\\w+")) {         if (word.charat(0) == '(‘ ) {             if (word.length() > 0) {                 sum +=line.get();             }             context.write(new text(word), new intwritable(sum));         }      } } 

you can find text between brackets without splitting or using regular expressions (assuming brackets closed, , don't have nested brackets):

int lastbracket = -1; while (true) {   int start = line.indexof('(', lastbracket + 1);   if (start == -1) {     break;   }   int end = line.indexof(')', start + 1);    system.out.println(line.substring(start + 1, end - 1);    lastbracket = start; } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -