user1048104
user1048104

Reputation: 3

How to Search a string array for specific strings

I am trying to search an array for a couple of specific strings that I get from words in a sentence. Eventually this sentence will be in-putted by the user but I have hard coded it in at the moment to make testing easier.If the program finds the strings it should return with "Yes" and "No" if it doesn't. The problem is that I am getting yes all the Time.

public class main {
public static void main(String[]args)
{

    String Sentence = "This is a sentence";
    String[] CensorList =
        {"big","head"};

    String[] words = Sentence.split(" ");
    System.out.println(words.length);
    boolean match = false;

    for(int i = 0; i < words.length; i++)
    {
        for (int j = 0; j < CensorList.length; j++)
        {
            if(words[i].equals(CensorList[j]))
            {
                match = true;
        }else{
            match = false;
        }
    }

    }
    if (match = true){
        System.out.println("Yes");}
    else{
        System.out.println("No");
}

} }

I would really appreciate any help with this one, Thanks in advance.

Upvotes: 0

Views: 114

Answers (6)

duffy356
duffy356

Reputation: 3716

the if in your second for() has wrong braces.

try this one:

for (int j = 0; j < CensorList.length; j++)
{
    if(words[i].equals (CensorList[j])) {
        match = true;
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }
    match = false;
}

for your second try:

the

if (match = true)

does not compare match with true, it sets the match flag to true, which results always in true.

compare the flag in your if:

if (match == true) // or simply if (match)
{ .... 

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388436

You can use a simple RegEx based solution for this

private static boolean test(String value) {
    String[] CensorList = { "This", "No" };

    for (String string : CensorList) {
        Pattern pattern = Pattern.compile("\\b" + string + "\\b", Pattern.CASE_INSENSITIVE);
        if (pattern.matcher(value).find()) {
            return true;
        }
    }
    return false;
}

Then

String string = "This is a sentence";
if(test(string)){
    System.out.println("Censored");
}

Upvotes: 1

Antoine
Antoine

Reputation: 558

Try to use

public class main {
public static void main(String[]args)
{

    String Sentence = "This is a sentence";
    String[] CensorList =
        {"This","No"};

    String[] words = Sentence.split(" ");
    System.out.println(words.length);
    boolean match = false;

    for(int i = 0; i < words.length; i++)
    {
        for (int j = 0; j < CensorList.length; j++)
        {
            if(words[i].compareTo(CensorList[j])==0)
            {
                System.out.println("Yes");
            }
            else{System.out.println("No");}

        }
    }

}

Upvotes: 0

LionC
LionC

Reputation: 3106

I think you have some typos in here.

    for (int j = 0; j < CensorList.length; j++)
    {
           if(words[i].equals (CensorList[j]));
    }

This will do essentially nothing, as the if has nothing to do if the expression is evaluated to true. Then after the loop you set match to true, so it will be true always, and it will always print "Yes"

Upvotes: 1

user123454321
user123454321

Reputation: 1058

Try it:

for(int i = 0; i < words.length; i++)
{
    for (int j = 0; j < CensorList.length; j++)
    {
        if(words[i].equals (CensorList[j])) 
            match = true;
    }
            if (match) {
                System.out.println("Yes"); }
            else {
                System.out.println("No"); }
            match = false;
}

Upvotes: 1

Miquel
Miquel

Reputation: 15675

Any reason you are not using String.indexOf(String) ?

Another issue is if you are doing this repeatedly for the same (very large) stirng in which case, you might want to look into more sophisticated algorithms like suffix trees or even use specialized software like Apache Lucene

Upvotes: 0

Related Questions