Counting the number of specific occurrences in a java String

J. Doe :

I am attempting to solve a problem where I create a method that counts the number of occurrences of capital and lowercase ("A" or "a") in a certain string. I have been working on this problem for a week now, and the main error that I am receiving is that "char cannot be dereferenced". Can anyone point me in the correct direction on this Java problem? Thank you.

class Main{ 
    public static int countA (String s)
    {
        String s1 = "a";
        String s2 = "A";
        int count = 0;
        for (int i = 0; i < s.length; i++){
            String s3 = s.charAt(i); 
            if (s3.equals(s1) || s3.equals(s2)){
                count += 1;
            }
            else{
                System.out.print("");
            }
        }
    }

   //test case below (dont change):
    public static void main(String[] args){
        System.out.println(countA("aaA")); //3
        System.out.println(countA("aaBBdf8k3AAadnklA")); //6
    }
}
Scary Wombat :

try a simpler solution

String in = "aaBBdf8k3AAadnklA";
String out = in.replace ("A", "").replace ("a", "");
int lenDiff = in.length () - out.length ();

Also as @chris mentions in his answer, the String could be converted to lowercase first and then only do a single check

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=473752&siteId=1