Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I loop this program back to prompt the user to enter another sentence all in the same run of the program? Here is

How would I loop this program back to prompt the user to enter another sentence all in the same run of the program? Here is my code:

1 import java.util.Scanner; 2 3 public class TEST { 4 private static Scanner console; 5 public static void main(String[] args) { 6 7 console = new Scanner(System.in); 8 9 String userStr=""; 10 11 // runs until user types "exit" 12 while(true) { 13 System.out.print("Enter a sentence: "); 14 userStr=console.nextLine(); 15 char[] userStrArray = userStr.toCharArray(); 16 int wordEndingWithVowel=0; 17 // when its a single character entered 18 if(userStr.trim().length()==1) { 19 20 switch(userStrArray[0]) { 21 case 'a': case'A': case 'e': case 'E': case 'i' : case 'I': case 'o' : case 'O': case 'u': case 'U': 22 wordEndingWithVowel+=1; 23 } 24 } 25 else { 26 // when word is a single word and ends in a vowe; 27 switch(userStrArray[userStrArray.length-1]) { 28 case 'a': case'A': case 'e': case 'E': case 'i' : case 'I': case 'o' : case 'O': case 'u': case 'U': 29 wordEndingWithVowel+=1; 30 31 } 32 } 33 // when the words in a symbol, space or non letter we count if the previous letter is vowel 34 for(int index=0;index0) { 37 switch(userStrArray[index-1]) { 38 case 'a': case'A': case 'e': case 'E': case 'i' : case 'I': case 'o' : case 'O': case 'u': case 'U': 39 wordEndingWithVowel+=1; 40 } 41 } 42 } 43 } 44 System.out.println("The number of words in userStr ending in vowels is: "+wordEndingWithVowel); 45 System.out.println("The number of words in userStrArray ending in vowels is: "+wordEndingWithVowel); 46 47 if(userStr.equals("exit")); { 48 break; 49 50 } 51 } 52 } 53 }

The output should look like this for clarification.

image text in transcribed

MMoM GRASP exec: java A8 .. 4MEnter a sentence: There's no place like home! MM4MThe number of words in userStr ending in vowels is: 5 MMaMThe number of words in userstrarray ending in vowels is: 5 MM&M MEnter a sentence (to stop, type exit) Show me the money! MM4MThe number of words in userStr ending in vowels is: 2 MM MThe number of words in userStxArray ending in vowels is: 2 MM&M MEnter a sentence (to stop, type exit): La-dee-da, LA-DEE-DA. MM4 MThe number of words in userStr ending in vowels is: 6 MM4MThe number of words in userStAxray ending in vowels is: 6 MM&M MEnter a sentence (to stop, type exit): exit MM4MThe total number of words ending in vowels is: 13 MM&MBye! MM&M MMvMiGRASP: operation complete

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions