Java - method to check whether a String contains a certain character

Asked

Viewed 4,075 times

-2

I have a problem with this method. It seems to me, whenever I try to use it with some String, I get false for the value of a, even if the string contains these three characters. Someone can tell me what’s wrong here?

 public static boolean VerSeTemLetras (String qqString){
    boolean a;
    int tem_a;
    int tem_b;
    int tem_c;

    if (qqString.contains("a")) {
        tem_a = 1;
    } else {
        tem_a = 0;
    }
    if (qqString.contains("b")) {
        tem_b = 1;
    } else {
        tem_b = 0;
    }
    if (qqString.contains("c")) {
        tem_c = 1;
    } else {
        tem_c = 0;
    }

    a = ((tem_a + tem_b + tem_c) > 2 );

    return a;
}
  • The purpose is to know if the String has a, b and c or is to find out whether it contains a, b or c?

  • Gives to greatly improve your code, what you want to know exactly?

2 answers

2


I think that’s better

public static boolean VerSeTemLetras (String qqString){
    return qqString.contains("a") && qqString.contains("b") && qqString.contains("c"); 
}

1

public static boolean verSeTemAeBeC(String qqString) {
    return qqString.contains("a") && qqString.contains("b") && qqString.contains("c");
}

public static boolean verSeTemAouBouC(String qqString) {
    return qqString.contains("a") || qqString.contains("b") || qqString.contains("c");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.