There is a task: to find headers in which there are words defined by an array, and to weed out of them headers with words from the second array.

mName - the title in which we are looking for words from the search array. In the second nested loop in 2 if -s, which for some reason I decided to decompose into 2 in order not to get confused, but I still get confused :( The logic lies with the second nosearch array checking
the old

for (int i = 0; i <= search.length - 1; i++) { if (mName.indexOf(search[i]) != -1) { for (int i1 = 0; i1 <= nosearch.length - 1; i1++) { boolean trigger = false; // System.out.println(i1); if (mName.indexOf(nosearch[i1]) != -1) { trigger = false; System.out.println(mName); // break; } else { trigger = true; } if (trigger) { // listVakansy.add(mName); System.out.println(mName); break; } } break; } } 

The problem is as follows: the nosearch array contains only the first header that has fallen, all the rest are freely passed to the exhaust.

Added by
I threw it separately and I will not figure it out

 public class TestStringSearch { public static void main(String[] args) { String[] search = {"тестер", "java", "системный", "cистемный", "тестировани", "тестировщик", "программист"}; String[] nosearch = {"инженер", "микроконтроллер", "микроконтроллёр", "1c", "1С", "1c", "c#", "с#"}; String[] str = {"ведущий java программист", "1c программист", "инженер программист 1с", "специалист по сопровождению и тестированию", "тестировщик", "интежер", "глюк"}; //перечисление слов for (int i = 0; i <= str.length - 1; i++) { //перечисление искомых слов (search) for (int i1 = 0; i1 <= search.length - 1; i1++) { //перечисление найденных слов if (str[i].indexOf(search[i1]) != -1) { //перечисление отрицаемых слов (nosearch) for (int i2 = 0; i2 <= nosearch.length - 1; i2++) { // если находим то прерываем if (str[i].indexOf(nosearch[i2]) != -1) { break; } else { System.out.println(str[i]); break; } } } } } } } 
  • one
    With 2 mfs obviously overdid ..... - Gorets
  • one
    I re-read the task a couple of times and do not catch up .... tell me what is and what should work out at the end, on some example ... and it’s better to solve the problem more elegantly by means of java ... without this heap of overdoings ... - Gorets
  • you need to scan the list of str selecting those that contain words from the search list, and from them remove words containing words from the nosearch list ie the exhaust must have filtered str from the search and nosearch lists ; I’m still trying to do it for “one pass” - hitman249

3 answers 3

Somehow you are too smart with breaks, it works like this:

 public class Titles { public static void main(final String[] args) { // titles final String[] titles = { "ведущий java программист", "1c программист", "инженер программист 1с", "специалист по сопровождению и тестированию", "тестировщик", "программист микроконтроллеров", "интежер", "глюк" }; // include list final String[] includes = { "тестер", "java", "системный", "cистемный", "тестировани", "тестировщик", "программист" }; // exclude list final String[] excludes = { "инженер", "микроконтроллер", "микроконтроллёр", "1c", "1С", "1c", "c#", "с#" }; // find occurences final List<String> result = find(titles, includes, excludes); // handle result for (final String s : result) { System.out.println(s); } } // все проверки аргументов опущены для сокращения кода public static List<String> find(final String[] titles, final String[] includes, final String[] excludes) { // prepare result list final List<String> result = new ArrayList<String>(titles.length); // find matches for (final String title : titles) { if (filter(title, includes) && !filter(title, excludes)) { result.add(title); } } // return result return result; } private static boolean filter(String title, String[] patterns) { for (String s : patterns) { if (-1 != title.indexOf(s)) { return true; } } return false; } } 

Result:

 ведущий java программист специалист по сопровождению и тестированию тестировщик 
  • brilliant! implementation is as simple as the door! heh, not soon on my level to such, knowledge of various subtleties is sorely lacking - hitman249
  • there is nothing ingenious there. just try to analyze the code that you write some time after writing. You yourself will find in it errors and pieces of code that can be rewritten. as well as a duplicate code - jmu 7:08 pm
 ... if (trigger) { // listVakansy.add(mName); System.out.println(mName); trigger = false;//break;//Думаю тут надо не break а trigger = false; } } //break;//А этот брейк зачем? 
  • added full clone algorithm. where is it wrong? - hitman249

We go on the first 2 lists:

 if (search[i].contains(nosearch[i]) { 

We go on the 3rd list:

 if (nosearch[i].contains(str[k])) { nosearch[i].replace(str[k], "") } 

Try to figure it out and finish what would work in the code ... The arrays in my code do not coincide with yours, but if I am stupid, the method should solve your problem.

  • for example: "maintenance and testing specialist" according to the first list will follow the word "testing" and you compare 100% match, but you need the content of any word from the search further along the third list (I recall the command from the original) listVakansy.add (mName ); those. after running through the first two for-am, all matches in the third for-e should not call the command given above, which will filter out hitman249
  • one
    Well, correct indexes and cycles, so that the word from the list that can be found is checked in each element of the array you are looking at - Gorets
  • Yes, I have already erased my fingers in the blood on the keyboard, I can’t fix it for 2 days - hitman249
  • one
    So what are you trying to do at random? It fails, draw an algorithm on a piece of paper, check it in your mind and program it - Gorets