11.1k views
4 votes
ou are working at the frozen yogurt shop in the mall. Your boss asks you to count how many flavors have the word chocolate in their name. You begin going through the list of flavors, but quickly realize that there are over 9000 flavors to check! Luckily, your boss stored all of the different flavors in a Java Array named flavorArray on the company computer. Write a Java program to count the number of flavors that have chocolate in their name.

User Fanfan
by
5.4k points

1 Answer

3 votes

Answer:

Following are the code to the given question:

int j,chocolateFlavorCount = 0;//declaring integer vvariable

for(j=0;j< flavorArray.length;j++)//defining a loop that starts from 1 to array length

{

if(flavorArray[i].contains("chocolate"))//use if that checks array contains string value that is chocolate

{

chocolateFlavorCount++;//incrementing integer variable value

}

}

System.out.println(chocolateFlavorCount);//printing integer variable

Step-by-step explanation:

  • In this question, an integer variable "chocolateFlavorCount, and j" is declared, in which j used in the for loop.
  • The loop starts from 1 and stops when it is equal to the array length it also defines an if block that checks array contains string value that is chocolate.
  • If the condition is true it will increment integer variable value and print its values.
User Robert Petz
by
4.9k points