in the string String str2 [] = name.split (""); give an extra character in the array ... Let me explain with an example "Aditya" .split ("") will return [, A, d, i, t, y, a] You will have an extra character in your array ...
"Aditya" .split ("") does not work as expected, using the saroj route, you will get an extra character in String => [, A, d, i, t, y, a].
I changed it, see below the code works as expected
public static boolean isValidName(String inputString) { String specialCharacters = " !#$%&'()*+,-./:;<=>?@[]^_`{|}~0123456789"; String[] strlCharactersArray = new String[inputString.length()]; for (int i = 0; i < inputString.length(); i++) { strlCharactersArray[i] = Character .toString(inputString.charAt(i)); } //now strlCharactersArray[i]=[A, d, i, t, y, a] int count = 0; for (int i = 0; i < strlCharactersArray.length; i++) { if (specialCharacters.contains( strlCharactersArray[i])) { count++; } } if (inputString != null && count == 0) { return true; } else { return false; } }
user4423251 Apr 25 '17 at 10:46 on 2017-04-25 10:46
source share