Question: Hi this is my code for the following task. Specification: Main class: StretchWith 2 Vowels Read sentences from the user until * is entered. Show

Hi this is my code for the following task.
Specification:
Main class: StretchWith2Vowels
Read sentences from the user until * is entered. Show the number of words in each sentence that contain a stretch of non-z characters with exactly 2 vowels. A stretch starts from the start of the word or after a 'z'. A stretch terminates just before another 'z' or at the end of the word.
Examples:
Matching words: zoo, azozooza, GONZALEZ
Non-matching words: ozo, azoooza
The sentences contain no punctuation, the words are separated by one or more spaces, and the characters may be upper or lower case. Keep reading sentences until the user enters "*".
Sample I/O:
Sentence: azoooza azooza zoo azoo
Matching words =3
Sentence: GONZALEZ passes the ball to VAZQUEZ
Matching words =3
Sentence: azozototzeti
Matching words =1
Sentence: *
Done
MY CODE//
import java.util.Scanner;
public class StretchWith2Vowels {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while (true){
int count =0;
System.out.print("Sentence: ");
String sentence = sc.nextLine();
if (sentence.equals("*")){
System.out.println("Done");
break;
}
String[] words = sentence.split("\\s+");
for (String word : words){
if (hasTwoVowelsInStretch(word)){
count++;
}
}
System.out.println("Matching words ="+ count);
}
sc.close();
}
static boolean hasTwoVowelsInStretch(String word){
boolean inStretch = false;
int vowelCount =0;
for (char ch : word.toCharArray()){
if (Character.toLowerCase(ch)=='z'){
inStretch = true; // Set inStretch when encountering 'z'
} else if (inStretch){
char lowercaseCh = Character.toLowerCase(ch);
if (lowercaseCh =='a'|| lowercaseCh =='e'|| lowercaseCh =='i'|| lowercaseCh =='o'|| lowercaseCh =='u'){
vowelCount++;
}
if (vowelCount >2){
return false; // More than 2 vowels in stretch, not a match
}
} else {
vowelCount =0; // Reset vowel count when not in stretch
}
}
return vowelCount ==2;
}
}
//
This is a split difference of my vs the correct output
DIFF SPLIT DIFF YOUR OUTPUT EXPECTED
Sentence: GONZALEZ passes the ball to VAZQUEZ
Sentence: GONZALEZ passes the ball to VAZQUEZ
Matching words =2
Sentence: azozotoezeti
Matching words =3
Sentence: azozotoezeti
Matching words =0
Sentence: azoooza azOtOza zoo AZoo
Matching words =1
Sentence: azoooza azOtOza zoo AZoo
Matching words =3
Sentence: ooze zeez zaap zapa zaza
Sentence: ooze zeez zaap zapa zaza
Matching words =4
Sentence: *
Done
 Hi this is my code for the following task. Specification: Main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!