0

while writing my program I have run into another nooby road block.

if(StringTerm[0].equals("wikipedia"))
        {
            StringBuilder SearchTermBuilder = new StringBuilder();
            for(int i = 1; i < StringTerm.length; i++)
            {
                SearchTermBuilder.append(StringTerm[i] + " ");
            }
            // This is the string it outputs. 
            WIKI_ID = "Wikipedia";
            SearchTerm = SearchTermBuilder.toString();
            SearchTermFull = WikiBaseLinkReference.WIKI_WIK + SearchTermBuilder.toString();
        }

This code checks for input from a console command "/wiki" and checks to see if the first string after the word "wiki" matches "wikipedia" and if so, it builds a string to match what I want it to do.

This is all well and good, and the program works fine, but I want users to be able to use different keywords to get the same results.

For Example: If I type /wiki wikipedia, it would do the same as /wiki pediawiki

If I made an array of different names called WIKIPEDIA

public static String WIKIPEDIA[] = {"wikipedia","pediawiki"};

How would I tell the if statement to check to see if the text entered equals one of the strings inside of my array? Every time I try to use an || or operator it throws me some errors.

Thanks in advance.

jason
  • 236,483
  • 35
  • 423
  • 525
  • Please follow the Java naming conventions, and use lowercase letters at the beginning of your variable names. It'll increase the readability of your code. – jlordo Aug 02 '13 at 20:49
  • 1
    Create a method that returns a `boolean` value and receives your array and checks if the value exists or not, and on that basis the method returns `true` or `false`. – Luiggi Mendoza Aug 02 '13 at 20:49
  • Use `java.util.List` instead of default arrays. `List` have a `contains` method, that could provide test that you want to apply. – aim Aug 02 '13 at 20:52

1 Answers1

1

You need a version of "any":

public boolean any(String[] array, String s) {
    for(String value : array) {
        if(s.equals(value)) { return true; }
    }
    return false;
}

Then

if(any(WIKIPEDIA, "wikipedia")) {
}
jlordo
  • 37,490
  • 6
  • 58
  • 83
jason
  • 236,483
  • 35
  • 423
  • 525
  • This works. It took me a minute to figure out what was going on in that though, and even now I'm not entirely sure. Thanks for the good answer. – user2647089 Aug 02 '13 at 20:59