Ask Question
18 February, 23:57

Write a program in Java that reads in text and prints as outputthe following:The number of words in the textThe number of sentences in the textThe number of times the letter "e" occurs in thetextThe number of times the letter "z" occurs in thetext

+4
Answers (1)
  1. 19 February, 00:26
    0
    See Below explanation with comments

    Explanation:

    public class HelloWorld {

    public static void main (String[] args) {

    //take any string as your text

    String text = "This is a sample text. Change your text here.";

    //Split it with space, that will gives all the words as an array of String

    String[] words = text. split (" ");

    //initialize variable with zero

    int eCount = 0;

    int zCount = 0;

    int sentenceCount = 0;

    / / iterate words array and increment the count

    for (String word : words) {

    //if a words ends with dot (.), means its end of sentence, increase sentence count

    if (word. endsWith (".")) {

    sentenceCount++;

    }

    / / here again iterate word to get every letter from word

    for (int i = 0; i
    //if its leeter e, increase eCount

    if (word. charAt (i) = = 'e') {

    eCount++;

    //if its leeter z, increase zCount

    }else if (word. charAt (i) = = 'e') {

    zCount++;

    }

    }

    }

    System. out. println ("Number of words in the text : "+words. length);

    System. out. println ("Number of sentence in the text : "+sentenceCount);

    System. out. println ("The number of times the letter / "e/" occurs in the text : "+eCount);

    System. out. println ("The number of times the letter / "z/" occurs in the text : "+zCount);

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program in Java that reads in text and prints as outputthe following:The number of words in the textThe number of sentences in the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers