3
public class HelloWorld{
    public static void main(String[] args){
        //create array with days of week. won't be modified
        String[] daysOfWeek = {"Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday","Sunday"};
        //pass the array to the eStatistics method so they check frequency of e
        eStatistics(daysOfWeek);
    }


    public static int[] eStatistics(String[] names){
        //create new array that will be the same size of the previous array but will have integers
        int[] newArray = new int[names.length];
        //go over each word (element) in the old array
        for(String word : names){
            System.out.println(word);
            //create a counter to store number of e's
            int counter = 0; //counter here so it resets every time we go over a different word
            int lengthOfWord = word.length();
            //go over each letter in the word
            for(int i = 0; i < lengthOfWord ; i++){
                if (word.charAt(i) == 'e'){ //if the letter is an 'e'
                    counter ++; //increment counter by 1
                }
            }
            // we should add the counter to the new array after we end counting the letter e in each word
            // how?
            // newArray[i] = counter;   ????
            System.out.println(counter);
        }
        return newArray;
    }
}

The purpose of this program is to count the frequency of 'e' in every word in the array daysOfWeek and return an array {0, 1, 2, 0, 0, 0, 0}. But how can I add the total of e's to the new array every time I finish counting how many there are in each word?

  • It can be done using other incremental variable, https://stackoverflow.com/questions/43470874/adding-one-to-each-element-in-an-array-using-an-enhanced-for-loop-in-java/43471260 – RGM Dec 13 '18 at 16:30

3 Answers3

1

You can do so using java-8, change the method to :

public static int[] eStatistics(String[] names) {
    int[] newArray = new int[names.length];

    for (int i = 0; i < names.length; i++) {
        newArray[i] = (int) names[i].chars().filter(ch -> ch == 'e').count();
    }

    return newArray;
}

Here we check the number of times each String has the character e and store the count at the corresponding index of the array.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
0

Use a traditional for loop

for (int i = 0; i < names.length; i++) {
   ....
   int lengthOfWord = names[i];
   ....
   newArray[i] = counter;    
}

If you use the loop variable i in your inner loop, change that to j or use j for the above.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0

You could utilize the Stream API and collect the result to an array, rather than use a traditional for loop:

public static int[] eStatistics(String[] names) {
    Function<String, Integer> countEs = string -> string.length() - string.replace("e", "").length();
    return Arrays.stream(names).map(countEs).mapToInt(i -> i).toArray();
}

To explain what this does:

  • The first line defines a Function which takes a String and returns an Integer. This function Counts the number of e's in the string. This function is taken from this answer and may be replaced by any other implementation doing the same thing.

The second line does the actual work:

  • Arrays.stream() simply creates a Stream from the array.
  • map() converts the String to an Integer using the function from the first line.
  • mapToInt() converts each Integer to an int. This is needed if you want to return an int[] and not a Integer[].
  • toArray() finally collect the Stream into an array.
Magnilex
  • 11,584
  • 9
  • 62
  • 84