Wednesday, November 11, 2009

Check whether the given string is number or alphabet

public boolean isNumber(String str) {



String strValidChars = "0123456789";

char strChar;

boolean isSuccess = true;



if (str.length() == 0) return true;



for (int i = 0; i < str.length() && isSuccess == true; i++) {

strChar = str.charAt(i);

if (strValidChars.indexOf(strChar) == -1) {

isSuccess = false;

}

}



return isSuccess;

}



public boolean isAlphabet(String str) {



String strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

char strChar;

boolean isSuccess = true;



if (str.length() == 0) return true;



for (int i = 0; i < str.length() && isSuccess == true; i++) {

strChar = str.charAt(i);

if (strValidChars.indexOf(strChar) == -1) {

isSuccess = false;

}

}

return isSuccess;

}

No comments:

Post a Comment