简体   繁体   中英

How to properly check if a string contains another string from a string array? - JAVA

I was trying to get program working and it runs and all but it seems to believe that every string contains words from a string array. I am using the openCSV library to try and go through a csv file that contains business names and people names in the same column and I am trying to make it so that all company names would appear as the second column and all people names would appear on a third column. The first column is just an identifying number.

for (String[] row : inputEntries)
    {
        for(int i = 0; i < dictionary.length; i++)
        {
            String rowEntry = row[1].toLowerCase();
            String dictionaryTerm = dictionary[i].toLowerCase();

            if(rowEntry.contains(dictionaryTerm))
            {
                String entries = row[0] + "," + row[1] + "," + "";
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This contained a Dictionary word");
                break;
            }
            else if (i == dictionary.length)
            {
                String entries = row[0] + "," + "" + "," + row[1];
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This did not contain a Dictionary word");
                break;
            }
        }
    }

It does output to a separate csv which is intended but it seems to think that everything is a business name so I get a file that looks identical to the original. What is a possible solution to this dilemma? Am I misusing the contains function?

Input

"11111111","John Smith"
"11111112","Wells Fargo Bank"
"11111113","Company name LLC"
"11111114","John Connor"

Output

"11111111","","John Smith"
"11111112","Wells Fargo Bank",""    
"11111113","Company name LLC",""    
"11111114","","John Connor"

So working a little more on it I was able to get it to sorta do what I wanted but the problem is that it seems to only be checking for the first term in the dictionary string. Here's the updated code:

    boolean match = false;
    boolean nomatch = false;
    int dicLength = dictionary.length;

    for (String[] row : inputEntries)
    {
        for(int i = 0; i < dicLength; i++)
        {
            String rowEntry = row[1].toLowerCase();
            String dictionaryTerm = dictionary[i].toLowerCase();

            match = rowEntry.contains(dictionaryTerm);

            if(match == true)
            {
                String entries = row[0] + "," + row[1] + "," + "";
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This contained a Dictionary word");
                match = false;
                break;
            }

            if (i == (dicLength - 1))
            {
                nomatch = true;
            }

            if (nomatch == true)
            {
                String entries = row[0] + "," + "" + "," + row[1];
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This did not contain a Dictionary word");
                match = false;
                break;
            }
        }
    }

Looking at your updated code, you are accidentally setting match = false rather than nomatch = false when you check nomatch == true. The best way to solve this would be to eliminate those variables completely and compact it to the following (also that break at the end isn't needed as for the loop is about to close):

    int dicLength = dictionary.length;

    for (String[] row : inputEntries) {
        for(int i = 0; i < dicLength; i++) {

            String rowEntry = row[1].toLowerCase();
            String dictionaryTerm = dictionary[i].toLowerCase();

            if(rowEntry.contains(dictionaryTerm)) {
                String entries = row[0] + "," + row[1] + "," + "";
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This contained a Dictionary word");
                break;
            }

            if (i == (dicLength - 1)) {
                String entries = row[0] + "," + "" + "," + row[1];
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This did not contain a Dictionary word");
            }
        }
    }

You can make this even more efficient by not checking your dic length every iteration, (force it outside of the loop) but that's up to you! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM