Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of specific occurrences in a java String

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
    }
}
like image 301
J. Doe Avatar asked Apr 27 '17 02:04

J. Doe


4 Answers

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

like image 111
Scary Wombat Avatar answered Oct 21 '22 07:10

Scary Wombat


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();
}
like image 34
Ousmane D. Avatar answered Oct 21 '22 06:10

Ousmane D.


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();
like image 27
Chris Avatar answered Oct 21 '22 06:10

Chris


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

int numA = string.replaceAll("[^aA]","").length();
like image 40
Keegan Teetaert Avatar answered Oct 21 '22 08:10

Keegan Teetaert