views:

140

answers:

6

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

+13  A: 

Use String.indexOf() instead:

public static boolean contains(String s, char value){
    return s != null && s.indexOf(value) > -1;
}

String sVal = "Jeff George";
assertTrue(contains(sVal, 'e'));
sVal = null;
assertFalse(contains(sVal, 'e'));
Péter Török
this worked brilliantly, there is no way i could have seen that. return if s doesnt equal null AND that the index of the value is greater than -1, meaning that it is there and the string isnt empty. thank you very much!
Curtis
+4  A: 

Why are you doing this? Your function is already implemented as a method on String. Use String.indexOf instead:

s.indexOf('a') == -1

I think Carl Manaster was right in the comments about your specific problem - you need to use assertFalse not assertTrue here:

String sVal  = "Jeff George";
boolean notIn = StringMethods.isThere(sVal, 'b');
assertFalse(notIn); // not assertTrue

As an aside, notIn is a terrible name for that variable - it means exactly the opposite of what it says. Maybe that is why you got confused.

Mark Byers
it should be assertFalse, you are correct, that is a typing error on my part, thank you
Curtis
+1  A: 

What problem are you encountering ?

Firstly,

  for(int x = 0; x <= s.length(); x++){

doesn't look right. x is going to run off the end of your string (use x < s.length() instead if you want to iterate through a string). But higher level functions are available for doing what you want (see the other answers here).

Brian Agnew
A: 

If String.indexOf(char) returns -1, hasA is false. Otherwise, it's true.

Jekke
+2  A: 

With Java 6 you can just do

final String s = "This is a test";
s.contains("x"); // False
s.contains("t"); // True
fuzzy lollipop
Two flaws: 1) It's actually Java 1.5. 2) It doesn't compile. It takes a `CharSequence`: http://java.sun.com/javase/6/docs/api/java/lang/String.html#contains%28java.lang.CharSequence%29
BalusC
A: 

Or, try StringUtils.contains() from apache commons - that will handle the null String case for you as well.

http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html#contains%28java.lang.String,%20char%29

elduff
apache commons is a poorly designed and implemented code cancer
fuzzy lollipop
Besides, using a 3rd party library for doing such elementary things, is, IMO, sheer madness. I mean, if you're going to stuff a jar in your app just to compare a String against `null` and do a `contains(...)` on it, I wonder how many 3rd party jars will be there when the app gets shipped! :)
Bart Kiers
+1 because a down-vote seems awful harsh. Apache commons is a widely accepted and used library. Is it way-overkill for just this one problem? Certainly. Has Java 1.5 and 1.6 made many of the functions in commons redundant? Yup. But sheesh...
Matthew Flynn