Answer to Question #124145 in Java | JSP | JSF for lexi

Question #124145
Java program to
(a) - Count the number of Words in the paragraph starts with the letter taken as input from the
user.
(b)- Display the total number of words in the paragraph
1
Expert's answer
2020-06-30T08:04:43-0400
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {

    private static int total;
    private static int numberOfWordsWithChar;
    private static String words[];

    private static char getChar() throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s.charAt(0);
    }

    private static String getParagraph() throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = "";
        String s1 = br.readLine();
        while (!s1.equals("")) {
            s +=  s1 + " ";
            s1 = br.readLine();
        }
        return s;
    }

    private static void process() throws IOException {
        words = getParagraph().split(" ");
        total = words.length;
        numberOfWordsWithChar = countWords(getChar());
    }

    private static int countWords(char ch) {
        int res = 0;
        for (String str : words) {
            if (str.startsWith(ch + "")) {
                res++;
            }
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        process();
        System.out.println("Total number of words: " + total);
        System.out.println("Total number of words that starts with special character: " + numberOfWordsWithChar);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS