1

This is my homework, I am trying to get the occurrences of 'e' in strings of the array. My method doesn't seem to work. Can anyone wise person advise where did I go wrong?

import java.util.*;
import java.lang.*;
import java.io.*;

public class CountChar {
    public static void main(String[] args) {
        String[] strings
                = {"hidden", "Java SDK", "DDD", "parameter", "polymorphism", "dictated", "dodged", "cats and dogs"};
        int result = numberOfCharacters(strings, 'e');
        System.out.println("No. of occurrences of 'e' is " + result);
    }

    public static String numberOfCharacters(String str, char a) {
        int aresult = 0;
        if (str.length() > 0) {
            aresult = count(str.substring(1), a) + (str.charAt(0) == a ? 1 : 0);
        }
        return aresult;
    }    
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • You are passing a `String[]` as an argument to `String` type parameter. Change the method parameter to `String[]`. – Rohit Jain Jan 18 '14 at 12:30
  • What is the `count` method? – assylias Jan 18 '14 at 12:30
  • Your method is written to work with a string not an array of string. You can keep your method as it is and call this method for every string in the array and save the sum in an variable. Then print the sum – Marco Acierno Jan 18 '14 at 12:57

3 Answers3

1

Just change your method signature to:

public static String numberOfCharacters(String[] str, char a) {

///code
}
Raj
  • 600
  • 1
  • 5
  • 18
0

+1 for santosh's answer. I have tried it in following way . Try it out

public class CountChar {
public static void main(String[] args) {
    String[] strings
            = {"hidden", "Java SDK", "DDD", "parameter", "polymorphism", 
               "eeeee", "dodged", "cats and dogs"};
    int result = 0;
    for(int i=0;i<strings.length;i++) {
        result += numberOfCharacters(strings[i], 'e');
    }
    System.out.println("No. of occurrences of 'e' in String is "+ result);
}
public static int numberOfCharacters(String str, char a) {
    int counter = 0;
    for( int i=0; i<str.length(); i++ ) {
        if( str.charAt(i)== a ) {
            counter++;
        } 
    }
    return counter;
}    

}

Vinayak Pingale
  • 1,315
  • 8
  • 19
0

In one liner:

Arrays.toString(strArray).replaceAll("[^"+a+"]", "").length();

Where 'a' is the char and 'strArray' the array of strings.

Using idea on how to count chars in a String from this question:

Java: How do I count the number of occurrences of a char in a String?

Complete code:

import java.util.Arrays;
public class CountChar {
    public static void main(String[] args) {
        String[] strings
                = {"hidden", "Java SDK", "DDD", "parameter", "polymorphism",
                  "eeeee", "dodged", "cats and dogs"};
        System.out.println("No. of occurrences of 'e' is " + 
                            countCharsInArray(strings, 'e'));
    }

    public static int countCharsInArray(String strArray[], char a) {
        return Arrays.toString(strArray).replaceAll("[^"+a+"]", "").length();
    }
}

Explaining it:

'Arrays.toString(strArray)' converts the array into a single string. 'replaceAll("[^"+a+"]", "")' replace all characters that are not the one we want by empty char '',

After that we only need to check the length of the resulting string.

Note: The array should not have nulls, or they should be removed first.

Community
  • 1
  • 1
Raul Guiu
  • 2,374
  • 22
  • 37