5

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
    }
}
Ousmane D.
  • 50,173
  • 8
  • 66
  • 103
J. Doe
  • 146
  • 5
  • first mistake `s.length` , second mistake `String s3 = s.charAt(i);` – Ousmane D. Apr 27 '17 at 02:14
  • What do you mean "first mistake". What should I change it to? @OusmaneMahyDiaw – J. Doe Apr 27 '17 at 02:15
  • for the first change to `s.length()` for the second change to `String s3 = Character.toString(s.charAt(i));` . another good solution is suggested by @ScaryWombat below within the anwer section. – Ousmane D. Apr 27 '17 at 02:16

4 Answers4

4

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

Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
4

the main error that I am receiving is that "char cannot be dereferenced"

change this:

s.length  // this syntax is incorrect

to this:

s.length()  // this is how you invoke the length method on a string

also, change this:

String s3 = s.charAt(i);   // you cannot assign a char type to string type

to this:

String s3 = Character.toString(s.charAt(i));  // convert the char to string

another solution to accomplishing your task in a simpler manner is by using the Stream#filter method. Then convert each String within the Stream to lowercase prior to comparison, if any Strings match "a" we keep it, if not we ignore it and at the end, we simply return the count.

public static int countA(String input)
{
    return (int)Arrays.stream(input.split("")).filter(s -> s.toLowerCase().equals("a")).count();
}
Ousmane D.
  • 50,173
  • 8
  • 66
  • 103
3

For counting the number of time 'a' or 'A' appears in a String:

public int numberOfA(String s) {
    s = s.toLowerCase();
    int sum = 0;
    for(int i = 0; i < s.length(); i++){
        if(s.charAt(i) == 'a')
            sum++;
    }
    return sum;
}

Or just replace everything else and see how long your string is:

int numberOfA = string.replaceAll("[^aA]", "").length();
Ousmane D.
  • 50,173
  • 8
  • 66
  • 103
Chris
  • 2,265
  • 4
  • 21
  • 49
1

To find the number of times character a and A appear in string.

int numA = string.replaceAll("[^aA]","").length();
Keegan Teetaert
  • 566
  • 5
  • 21