Counting the number of specific occurrences in a java String
Asked Answered
B

4

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
    }
}
Bridegroom answered 27/4, 2017 at 2:12 Comment(3)
first mistake s.length , second mistake String s3 = s.charAt(i);Norby
What do you mean "first mistake". What should I change it to? @OusmaneMahyDiawBridegroom
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.Norby
H
5

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

Haag answered 27/4, 2017 at 2:16 Comment(1)
Very intuitive solution here!Bridegroom
B
4

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();
Brahmani answered 27/4, 2017 at 2:17 Comment(0)
N
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();
}
Norby answered 27/4, 2017 at 2:31 Comment(0)
S
1

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

int numA = string.replaceAll("[^aA]","").length();
Sixpence answered 27/4, 2017 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.