Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper and lower case) and digit ('1' to '3') characters in a String. The method header is as follows:
public static int countNumAlphabetic ( String str ) { }
Sample Output:
countNumAlphabetic("mA13Th9zB86") returns 8
public static int countNumAlphabetic(String str) { int count = 0; for (int i = 0; i < str.length(); i++) { if (Character.isAlphabetic(str.charAt(i)) || str.charAt(i) == '1' || str.charAt(i) == '2' || str.charAt(i) == '3') ++count; } return count; }
public class CountNumAlphabetic { public static int countNumAlphabetic(String str) { int count = 0; for (int i = 0; i < str.length(); i++) { if (Character.isAlphabetic(str.charAt(i)) || str.charAt(i) == '1' || str.charAt(i) == '2' || str.charAt(i) == '3') ++count; } return count; } public static void main(String[] args) { System.out.println(countNumAlphabetic("mA13Th9zB86")); } }
Get Answers For Free
Most questions answered within 1 hours.