13.9k views
1 vote
BJP4 Self-Check 7.16a: countStrings Language/Type: Java arrays Strings Author:Marty Stepp (on 2016/09/08) Write a method countStrings that takes an array of Strings and a target String and returns the number of occurences target appears in the array.

User Ruslan
by
6.3k points

1 Answer

1 vote

Answer

//countStrings Method

public class countStrings {

public int Arraycount(String[] arrray, String target) {

int count = 0;

for(String elem : arrray) {

if (elem.equals(target)) {

count++;

}

}

return count;

}

// Body

public static void main(String args [] ) {

countStrings ccount = new countStrings();

int kount = ccount.Arraycount(new String[]{"Sick", "Health", "Hospital","Strength","Health"}, " Health"

System.out.println(kount);

}

}

The Program above is written in Java programming language.

It's Divided into two parts

The first is the method countStrings

While the second part of the program is the main method for the program execution

User Tess
by
6.1k points