0

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.

asked Apr 25, 2013 at 13:15
1
  • haven't checked your code but you definitely wanna remove that ';' after if().. Commented Apr 25, 2013 at 13:20

6 Answers 6

2

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)
{ .... 
answered Apr 25, 2013 at 13:18

2 Comments

Thanks this worked great, I have updated my code to print out the yes/no message only once. But its back to only printing yes again. I don't suppose you could point me in the right direction with this error. Sorry for the hassle.
check my answer, it might be interresting for you ;)
1

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;
}
answered Apr 25, 2013 at 13:18

Comments

1

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"

answered Apr 25, 2013 at 13:19

Comments

1

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");
}
answered Apr 25, 2013 at 13:20

Comments

0

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

answered Apr 25, 2013 at 13:17

Comments

0

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");}
 }
 }
}
answered Apr 25, 2013 at 13:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.