Write a complete JAVA method called countWordsThatDoNotContainChar. The method:
ID: 3594961 • Letter: W
Question
Write a complete JAVA method called countWordsThatDoNotContainChar. The method:
takes a String array and a char as a parameter
returns an int that is the number of Strings in the array that do NOT contain that char
For example, if you invoke the method with {"hello", "there", "bye"} and 'q', the method will return 3, because all three words do not contain the letter z. If you invoke with the same array and 'h' the method will return 1, because "bye" does not contain the letter h, but "hello" and "there" do contain the letter h.
For full credit, use good principles of code design. When possible, use existing methods!
Explanation / Answer
import java.io.*;
public class Demo6{
public static int countWordsThatDoNotContainChar(String[] s,char a){
int count = 0;
for (int i = 0; i<s.length; i++){
if (s[i].indexOf(a) < 0)
count++;
}
return count;
}
public static void main(String[] args){
String[] s = new String[3];
s[0] = "hello";
s[1] = "there";
s[2] = "bye";
System.out.println(countWordsThatDoNotContainChar(s,'q'));
System.out.println(countWordsThatDoNotContainChar(s,'h'));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.