Write a Java Program where program asks user to keep enterin
Write a Java Program where program asks user to keep entering words as long as the user doesnt type exit. When user enters exit the program will stop and display the total number of words entered and how many letter \"e\"s were entered and how many words contained more than one letter e.
Solution
WordECount.java
import java.util.Scanner;
public class WordECount {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = \"\";
int wordCount =0;
int eLetterCount = 0;
int wordContainE = 0;
int tempECount = 0;
while(!s.equalsIgnoreCase(\"exit\")){
System.out.print(\"Enter String: \");
s = scan.next();
tempECount = 0;
if(!s.equalsIgnoreCase(\"exit\")){
wordCount++;
for(int i=0;i <s.length() ; i++){
if(s.charAt(i) == \'e\'){
tempECount++;
}
}
eLetterCount = eLetterCount + tempECount;
if(tempECount > 1){
wordContainE++;
}
}
}
System.out.println(\"The total number of words entered: \"+wordCount);
System.out.println(\"How many letter \\\"e\\\"s were entered: \"+eLetterCount);
System.out.println(\"How many words contained more than one letter e: \"+wordContainE);
}
}
Output:
Enter String: Suresh
Enter String: MUrapaka
Enter String: Elephant
Enter String: peecock
Enter String: exit
The total number of words entered: 4
How many letter \"e\"s were entered: 4
How many words contained more than one letter e: 1
