I'm trying to make a program that will count the special characters in a given string. But it is not working. Here's my code.
public static final String []specialChars = {"!", "@", "#", "$", "%", "^", "&", "*", "(", ")",
"[", "]", "|", ";", "'", ",", ".", "/", "{", "}",
"\\", ":", "\"", "<", ">", "?" };
I used array because i'm trying to trace the matched special character in a String. So I decided to do the loop.
int specialCharCount = 0;
for ( int x = 0; x < specialChars.length ; ++x) {
specialCharCount = password.length() - password.replaceAll("\\specialChars[x]", "").length();
}
System.out.print(" and " + specialCharCount + " special characters.\n\n");
It is running but it gives me this output:
and 0 special characters.