1

I am trying to make a method called Baller(String str, char chr) which should return a boolean if the word contains the character. In the main method I have:

public static void main(String[] args) {
    Balling ball= new Balling ();
    System.out.println( ball.contains("Baller", 'a'));
    System.out.println( ball.contains("Baller", 'A'));
}

What I did was this but it did not work:

public boolean contains(String str, char chr ) {
    if(str.length() == chr) {
        return true;
    }
    else {
        return false;
    }       
}

What could be the problem?

ANSWER

public boolean contains(String str, char chr ) {

        for(int i = 0; i < str.length(); i++)
            if(str.charAt(i) == chr)
                return true;
        return false;
    }
}
Thrillofit123
  • 195
  • 1
  • 2
  • 12
  • Why are you comparing `str.length()` with a `char`? – dognose Aug 11 '15 at 19:33
  • @Tom I have check that thread and it wasn't the same as I was looking for because I already have a main method which I should continued with. – Thrillofit123 Aug 11 '15 at 19:33
  • So you compare the length of the string (6) with 'a' ? – JFPicard Aug 11 '15 at 19:34
  • @dognose As you see. im kinda new at this. I was thinking more like all the letters in string compare if there is a char letter in the string. – Thrillofit123 Aug 11 '15 at 19:34
  • @Thrillofit123 If you have a `main` method or not is not the point here. The point is how you can check if a single character appears in a given string. And the linked question answers how you can fix your `contains` method. – Tom Aug 11 '15 at 19:35

2 Answers2

5

The problem is that string length is compared to character value:

str.length() == chr

What you should use if the indexOf String method

public boolean contains(String str, char chr) {
  return str.indexOf(chr) != -1;
}
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • This worked. It was really simple. But I bet there is another way. but this is much more easier. but if you have time. could you explain it easier for me. I read the documentation and I did not understand full. but I would appreaciate if you could tell me like in a basic step by step :)? – Thrillofit123 Aug 11 '15 at 19:41
  • Other solution would be to convert String to character array and compare each character to given one. Above solution uses `indexOf(char)` method which finds the character in the string and return its position. If character is not found then value '-1' is returned. So any index but `-1` indicates that the string contains that character. – MaxZoom Aug 11 '15 at 19:52
0

You are testing if the length of the String is equal to some char. This will perhaps work if your String is of length zero and your character == 0 or perhaps if your String is of length one and you compare it to the character with the numeric value of 1. In other words: Almost never.

You never return false. So all your string contain all your characters.

String has an indexOf( character ) method. use that. See documentation.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58