Question
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;index
The output should look like this for clarification.
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 completeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started