public class Strings { public static void main(String[] args) { String[] a = {"The", "in", "the", "green", "Hat"}; int num; // Test the second method to count the occurrences of a character // in an array of strings num = countChars2(a, 'e'); System.out.println( num); // Count the strings longer than 3 in the array num = countLarger(a, 3); System.out.println( num); // Count the strings longer than 3 in the command line arguments num = countLarger(args, 3); System.out.println( num); // Count the 'e's in the array num = countChars(a, 'e'); System.out.println( num); } // Can we simplify this method? // A second method to count the occurrences of a character // in an array of strings static int countChars2(String[] b, char target){ int ans = 0; for (int i = 0; i < b.length; i++){ String s = b[i]; int charsInStringS = 0; for (int j = 0; j < s.length(); j++){ if (target == s.charAt(j)){ charsInStringS = charsInStringS + 1; } } ans = ans + charsInStringS; } return ans; } // A method to count the occurrences of a character in a string static int countCharsInString(String s, char target){ int ans = 0; for (int i = 0; i < s.length(); i++){ if (target == s.charAt(i)){ ans = ans + 1; } } return ans; } // A method to count the occurrences of a character // in an array of strings static int countChars(String[] b, char target){ int ans = 0; for (int i = 0; i < b.length; i++){ String s = b[i]; // Call a helper method ans = ans + countCharsInString(s, target); } return ans; } // A method to count the number of strings longer than a limit static int countLarger(String[] b, int limit){ int ans = 0; for (int i = 0; i < b.length; i++){ if (b[i].length() > limit){ ans = ans + 1; } } return ans; } }