Replacing if statement for for loop (Java)

I was working on a project, and one of the comments I received was that my if statement was too long. I agree with this, but I'm still confused how to replace it with the for loop that was suggested. It drove me crazy. The project was to analyze the consonants in the proposal and report if they are in it. Here is my code that I used. The project was noted in such a way that it is rather “where is my mistake / where can the question be improved”.

if ((userInput.contains("b"))||(userInput.contains("c"))||(userInput.contains("d"))||
    (userInput.contains("f"))||(userInput.contains("g"))||(userInput.contains("h"))||
    (userInput.contains("j"))||(userInput.contains("k"))||(userInput.contains("l"))||
    (userInput.contains("m"))||(userInput.contains("n"))||(userInput.contains("p"))||
    (userInput.contains("q"))||(userInput.contains("r"))||(userInput.contains("s"))||
    (userInput.contains("t"))||(userInput.contains("v"))||(userInput.contains("w"))||
    (userInput.contains("x"))||(userInput.contains("y"))||(userInput.contains("z")))
        //checking for consonants
        results += "The input contains consonants";
else 
    results += "The input contains no consonants";
+3
source share
10 answers

Well, time to learn Regular Expression or RegEx. So the you if statement:

if (userInput.matches(".*[bcdfghjklmnpqrstvwxyz].*"))
   results += "The input contains consonants";
else 
   results += "The input contains no consonants";

Read more in this Java Regex tutorial - Tutorial

+1

(, ) , , - ( -)

boolean hasConsonants(String userInput) {
    for (String consonant : consonants)
        if (userInput.contains(consonant)
            return true;
    return false;
}

if (hasConsonants(userInput))
    results += "The input contains consonants";
else
    results += "The input contains no consonants";

P.S. , , consonants char[] char String

+5

for, - :

for (char c = 'b'; c <= 'z'; c++) {
    if (c == 'e' || c == 'i' || c == 'o' || c == 'u')
        continue;
    if (userInput.contains(String.valueOf(c))) {
        return "The input contains consonants";
    }
}
return "The input contains no consonants";
+2

:

private String[] mCharacters = new String[] { "a", "b", "c" };

public boolean hasSelectedCharacters(String str) {
    for (final String c : mCharacters) {
        if (str.contains(c)) {
            return true;
        }
    }
    return false;
}

, true, false. , .

+1
boolean containsConsonant = 
    Pattern.compile("[bcdfghjklmnpqrstvwxyz]").matcher(userInput).find();
+1

- :

if(java.util.regex.Pattern.compile("(?i)[b-df-hj-np-tv-z]").matcher(line).find())
    System.out.println("Contains consonants");
0
char[] consonants = "bcdfghkjlmnpqrstvwxyz".toCharArray();
results = "The input contains no consonants";

for (int i = 0; i < consonants.length; i++)
    if (userinput.contains(String.valueOf(consonants[i])))
        results = "The input contains consonants";
0

( , ):

public static boolean containsConsonants(String s) {
    for (char c : "bcdfghjklmnpqrstvwxyz".toCharArray())
        if (s.indexOf(c) >= 0) return true;
    return false;
}

, :

private static final Pattern consonantPattern =
    Pattern.compile("[a-z&&[^aeiou]]");
public static boolean containsConsonants(String s) {
    return consonantPattern.matcher(s).find();
}
0

, :

if (userInput.matches (".*[bcdfghjklmnpqrstvwx]+.*"))
    results += "has consonants";
else
    results += "no consonants";

If you can guarantee that the characters will not be used, a simpler version will be:

if (!userInput.matches (".*[aeiou]+.*"))
    results += "has consonants";
else
    results += "no consonants";

Setting for case sensitivity.

-1
source

It will be more efficient and readable.

if(Pattern.matches("[b-df-hj-np-tv-z]+",userInput)){  
    ----code----
}
else{
    ----other code----  
}
-1
source

All Articles