Calculate the word from the String

Calculate the word from the String

Previous Home Next

 

Following program for Calculate the word from the Stirng.

 

 

/*
 * Save as a CalculateString.java
 * Program for Calculate the single word from the String.
 */
package r4r.co.in;

import java.io.*;

public class CalculateString {

    public static void main(String[] args) throws IOException {

        //Input the Srting from console, not exceed than 150 words.
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 250);
        System.out.println("Write a short Paragraph(>250 words): ");
        String inputText = reader.readLine();

        System.out.println("\nEnter the Word wise to find in the String: ");
        String string2 = reader.readLine();

        //Initialized variable
        int index = 0;                  //set String index positon to 0.
        int wordCount = 0;             //Set Word's counting to 0.
        String stringCount = string2;

        index = inputText.indexOf(stringCount);   // Find first Word in String
        while (index >= 0) {
            ++wordCount;
            index += stringCount.length();     // Examin the String until last word not read.
            index = inputText.indexOf(stringCount, index);
        }
        System.out.println(string2 + " word present in String " + wordCount + " times");
    }
}

Write a short Paragraph(>250 words):
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995.

Enter the Word wise to find in the String:
a
a word present in String 11 times

Previous Home Next