2

How can I check each character in a character array against a string? For example, I want to check these characters: !,@,#,$,%,^,&,*,(,),_,+ against the string af!a$co. Without regex, though.

Does this make sense?

public boolean IllSymbols(String newString)
{
   char [] data = {'!', '#', '$', '%', '^', '&', '*', '(', ')' };
   String str = new String(data);
   for (int i=0; i<str.length(); i++)
   {
      for (int j=0; j<local.length(); j++)
      {
        if (str.charAt(i)==local.charAt(j))
        {
             return true;
         }

       }

   }
   return false;
}

But when I check the word af!a$co in my main method

if (IllSymbols(newString)==true)
{
  return false; // false = text is illegal
}
else
{
  return true; // true = text is legal
}

The devil returns the string as 'true', meaning that it's a legal string. Maybe I'm doing something wrong?

2
  • 1
    Your last piece of code is pretty clumsy. It can be simplified as return !IllSymbols(newString);. Complex boolean logic, huh? :) Commented Oct 16, 2011 at 1:08
  • 3
    Your code doesn't look at the string that is passed in. It is comparing str to local, not to newString. Commented Oct 16, 2011 at 1:20

4 Answers 4

3
public boolean hasIllegalCharacters(String newString) {
  boolean hasIllegalChars = false;
  char[] illegalChars = {'!', '#', '$', '%', '^', '&', '*', '(', ')' };

  for (int i = 0; i < newString.length; i++) {
    for (int j = 0; j < illegalChars.length; j++) {
      if (newString.charAt(i) == illgalChars[j]) {
        hasIllegalChars = true;
      }
    }
  }

  return hasIllegalChars;
}

The idea here is that you start by assuming it's going to be false, and only set it to true if you find an illegal character.

If you want to return as soon as the first illegal char is found, then right after the line hasIllegalChars = true; you can add a break; statement.

To look at the opposite case, if you want to return true only if newString contains nothing but illegal characters, then you could flip things around, start by assuming it's true and initializing hasIllegalChars to true, and then setting it to false inside the nested for loops if anything that is not an illegal character is found, with this code (the for loops and method declaration is the same):

  hasIllegalCharacters = true;

  ...nested for loops...
    if (newString.charAt(i) != illegalChars[j]) {
      hasIllegalChars = false;
      // optional "break;" or "return false;" statement here
    }
  ...

  return hasIllegalChars;

...but in that case I would recommend renaming the method to "isAllIllegalCharacters" or something similar.

Sign up to request clarification or add additional context in comments.

1 Comment

Or if you want to return as soon as you find an illegal character, instead of a break put in a return.
1
public boolean illSymbols(String newString) {
    char [] data = {'!', '#', '$', '%', '^', '&', '*', '(', ')' };
    for(int i=0; i<data.length; i++) {
        if(newString.indexOf(data[i]) > -1) { // if the string contains the character
            return false;                     // return false
        }
    }
    return true;
}

Comments

1

Are you trying to check if some String contains any of the characters !, @, #, $, %, ^, &, *, (, ), _, +, in which case it's illegal? For pure ease of use and understanding, I'd suggest Guava's CharMatcher class.

private static final CharMatcher ILLEGAL_SYMBOLS = 
    CharMatcher.anyOf("!@#$%^&*()_+");

public boolean isLegal(String string) {
  return ILLEGAL_SYMBOLS.matchesNoneOf(string);
}

Comments

0

I prefer to use .contains()

....

for(int i=0; i < check.length; i++){

    for(int j=0; j < illegalChar.length; i++){
        if(check[i].contains(illegalChar[j])
        return false;
    }

}

return true;

....

Comments

Your Answer

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.