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