The main error I get is that "char cannot be dereferenced"
change this:
s.length // this syntax is incorrect
:
s.length()
also change this:
String s3 = s.charAt(i);
:
String s3 = Character.toString(s.charAt(i));
another solution to accomplish your task is simpler using the Stream#filter method. Then convert each String in Stream to lowercase before comparison, if any strings match "a" , we save it if we don't ignore it, and in the end we just return the count.
public static int countA(String input) { return (int)Arrays.stream(input.split("")).filter(s -> s.toLowerCase().equals("a")).count(); }
source share