How to test whether a char is NOT in a string? (java, junit)

Posted by JB on Stack Overflow See other posts from Stack Overflow or by JB
Published on 2010-03-24T21:10:28Z Indexed on 2010/03/24 21:13 UTC
Read the original article Hit count: 198

Filed under:
|
|
|

As title says, im having trouble with my junit tests passing for checking if a character is not in a string and how to check if an empty string doesnt have a character. here is the method i have:

     public static boolean isThere(String s, char value){
  for(int x = 0; x <= s.length(); x++){
   if(s.charAt(x) == value){
    return true;
   } else if(s.length() == 0){
    return false;
   }
  }
  return false;

And here is the junit test:

    public void testIsThere() {
  {
   String sVal  = "Jeff George";
   boolean hasA = StringMethods.isThere(sVal,'e');
   assertTrue(hasA);
   boolean hasE = StringMethods.isThere(sVal, 'o');
   assertTrue(hasE);
   boolean notIn = StringMethods.isThere(sVal,'b');
   assertTrue(notIn);
  }
  {
   String sVal  = "";
   boolean nothingIn = StringMethods.isThere(sVal,'a');
   assertFalse(nothingIn);
   boolean notIn = StringMethods.isThere(sVal,'b');
   assertFalse(notIn); 
  }
 }

Thank you very much, appreciated

© Stack Overflow or respective owner

Related posts about java

Related posts about junit