Looking back at the original question, we need to find some given keywords in a given sentence, count the number of occurrences and know something about where. I don’t quite understand what «where» means (is it an index in the sentence?), so I’ll pass that one… I’m still learning java, one step at a time, so I’ll see to that one in due time
It must be noticed that common sentences (as the one in the original question) can have repeated keywords, therefore the search cannot just ask if a given keyword «exists or not» and count it as 1 if it does exist. There can be more then one of the same. For example:
// Base sentence (added punctuation, to make it more interesting):
String sentence = "Say that 123 of us will come by and meet you, "
+ "say, at the woods of 123woods.";
// Split it (punctuation taken in consideration, as well):
java.util.List<String> strings =
java.util.Arrays.asList(sentence.split(" |,|\."));
// My keywords:
java.util.ArrayList<String> keywords = new java.util.ArrayList<>();
keywords.add("123woods");
keywords.add("come");
keywords.add("you");
keywords.add("say");
By looking at it, the expected result would be 5 for «Say» + «come» + «you» + «say» + «123woods», counting «say» twice if we go lowercase. If we don’t, then the count should be 4, «Say» being excluded and «say» included. Fine. My suggestion is:
// Set... ready...?
int counter = 0;
// Go!
for(String s : strings)
{
// Asking if the sentence exists in the keywords, not the other
// around, to find repeated keywords in the sentence.
Boolean found = keywords.contains(s.toLowerCase());
if(found)
{
counter ++;
System.out.println("Found: " + s);
}
}
// Statistics:
if (counter > 0)
{
System.out.println("In sentence: " + sentence + "n"
+ "Count: " + counter);
}
And the results are:
Found: Say
Found: come
Found: you
Found: say
Found: 123woods
In sentence: Say that 123 of us will come by and meet you, say, at the woods of 123woods.
Count: 5
Following Java example program used to search for the given word in the file.
Step 1: Iterate the word array.
Step 2: Create an object to FileReader and BufferedReader.
Step 3: Set the word wanted to search in the file. For example,
String input=”Java”;
Step 4: Read the content of the file, using the following while loop
while((s=br.readLine())!=null)
Step 5: Using equals() method the file words are compared with the given word and the count is added.
Step 6: The count shows the word occurrence or not in the file.
FileWordSearch.java
package File;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileWordSearch
{
public static void main(String[] args) throws IOException
{
File f1=new File("input.txt"); //Creation of File Descriptor for input file
String[] words=null; //Intialize the word Array
FileReader fr = new FileReader(f1); //Creation of File Reader object
BufferedReader br = new BufferedReader(fr); //Creation of BufferedReader object
String s;
String input="Java"; // Input word to be searched
int count=0; //Intialize the word to zero
while((s=br.readLine())!=null) //Reading Content from the file
{
words=s.split(" "); //Split the word using space
for (String word : words)
{
if (word.equals(input)) //Search for the given word
{
count++; //If Present increase the count by one
}
}
}
if(count!=0) //Check for count not equal to zero
{
System.out.println("The given word is present for "+count+ " Times in the file");
}
else
{
System.out.println("The given word is not present in the file");
}
fr.close();
}
}
Output:
The given word is present for 2 Times in the file
This tutorial explains how to read a text file and search any specific word in java. In order to search any specific word from a text file, we need to first read text file. Here we have provided simple example for reading a text file and search any specific word from that.
sample.txt
we are going to use below text file content using java code.
Simple Form Validation In Reactjs Example Create a Dropdown Menu using ReactJS Installing React Native on Windows Tutorial Create Dropdown Menu In React Native How To Customize Button In React Native Facebook loading animation using CSS3 Facebook Style Chat Application with jQuery and CSS Append Or Prepend HTML Using ReactJS Create Ripple Animation Effect Using Css Ripple Effect Animation On Button Click With CSS3
FileWordSearch.txt
package demo; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class FileWordSearch { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String[] words = null; // Intialize the word Array // Creation of File Descriptor for input file File f1 = new File("sample.txt"); // Creation of File Reader object FileReader fr = new FileReader(f1); // Creation of BufferedReader object BufferedReader br = new BufferedReader(fr); String s; String input = "Facebook"; // Input word to be searched int count = 0; // Intialize the word to zero // Reading Content from the file while ((s = br.readLine()) != null) { words = s.split(" "); // Split the word using space for (String word : words) { if (word.equals(input)) { // Search for the given word count++; // If Present increase the count by one } } } if (count != 0) { // Check for count not equal to zero System.out.println("The given word is present for " + count + " Times in the file"); } else { System.out.println("The given word is not present in the file"); } fr.close(); } }
Output:-
——————
This is all about Java Program to Search for a given word in a File. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
In this post, we are finding a word or substring in the String. The String is a sequence of characters and a class in Java.
To find a word in the string, we are using indexOf() and contains() methods of String class.
The indexOf()
method is used to find an index of the specified substring in the present string. It returns a positive integer as an index if substring found else returns -1.
The contains()
method is used to check whether a string contains the specified string or not. It returns a boolean value either true or false. If the specified string is found then it returns true, false otherwise.
Time for an Example:
Let’s create an example to find a word in the string. Here, we are using indexOf() method that returns an index of the specified substring. See the example below.
public class Main {
public static void main(String[] args){
String str = "This sentance contains find me string";
System.out.println(str);
// find word in String
String find = "find me";
int i = str.indexOf(find);
if(i>0)
System.out.println(str.substring(i, i+find.length()));
else
System.out.println("string not found");
}
}
This sentance contains find me string
find me
Example 2
Let’s create another example to find a word in the string. Here, we are using the contains()
method that returns true, if the specified string is found. See the example below.
public class Main {
public static void main(String[] args){
String str = "This sentance contains find me string";
System.out.println(str);
// find word in String
String find = "find me";
boolean val = str.contains(find);
if(val)
System.out.println("String found: "+find);
else
System.out.println("string not found");
}
}
This sentance contains find me string
String found: find me
Problem Description
How to search a word inside a string?
Solution
This example shows how we can search a word within a String object using indexOf() method which returns a position index of a word within the string if found. Otherwise it returns -1.
public class SearchStringEmp{ public static void main(String[] args) { String strOrig = "Hello readers"; int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1) { System.out.println("Hello not found"); } else { System.out.println("Found Hello at index " + intIndex); } } }
Result
The above code sample will produce the following result.
Found Hello at index 0
This example shows how we can search a word within a String object
public class HelloWorld { public static void main(String[] args) { String text = "The cat is on the table"; System.out.print(text.contains("the")); } }
Result
The above code sample will produce the following result.
true
java_strings.htm
In this tutorial, we will examine the possibility to search a Microsoft Word document for a text / pattern, using a Java program. We will use Apache POI
to parse the Word document and java.util.Scanner
to perform a string search, and report the number of occurrences of the input pattern as the output. This tutorial is targeted for beginners, and you can feel free to extend the code provided in this post for your project requirements. The list of steps for this guide is captured below:
Step by Step Guide to Search Word Document in Java |
1.Read Word Document in Java
In order to search the contents of a Word document, it is required to read the document as an object of type java.io.FileInputStream
first. The code to do this is straight forward, and is shown below:
FileInputStream input_document = new FileInputStream(new File("test_document.doc"));
2. Parse Document Text Using Apache POI
In this step, we will use WordExtractor
, defined in org.apache.poi.hwpf.extractor.WordExtractor
to extract the contents of the Word document. To create an object of type WordExtractor, we will pass the FileInputStream object, created in Step – 1. Apache POI has made this class available for all Word to Text conversion necessities. The code to initialize the WordExtractor object is shown below:
WordExtractor my_word=new WordExtractor(input_document);
3.Create Scanner / Define Search Pattern
Once you have created the WordExtractor object, you can pass the entire Word document text to the Scanner
class, defined in java.util.Scanner
by passing the document text as a string, using getText
method in WordExtractor. You should define the search pattern at this stage using java.util.regex.Pattern
class. This also gives you the power to use regular expressions in your search. For now, let us focus on a simple example. We will count the number of times the word “search” is present in our test document. The Java code is provided below:
Scanner document_scanner = new Scanner(my_word.getText());
Pattern words = Pattern.compile("(search)");
4.Perform Search / Find All Matches
Using the Scanner object created earlier, we iteratively loop through the document text using hasNextLine
method. While scanning every line, we use findInLine
method and pass the pattern created earlier to see if the search filter is present in the scanned line. We increment the match counter by 1 for a match, otherwise we scan the next line. The search word can be found more than once within a same line, so we use the next
method in scanner object to match all occurrences.
while (document_scanner.hasNextLine()) {
key = document_scanner.findInLine(words);
while (key != null) {
document_scanner.next();
count ++;
key = document_scanner.findInLine(words);
}
document_scanner.nextLine();
}
5.Output Search Result
You are now ready to output the search result at this stage. It is just a print of the number of times the match was found, using a simple SOP statement.
System.out.println("Found Input "+ count + " times");
Search Word Document using Java – Complete Program
The complete code to implement a search functionality in Microsoft Word documents using Java language is shown below. You can treat this as a prototype and extend it further for any search needs.
import java.io.FileInputStream;
import java.io.*;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.MatchResult;
public class searchWord {
public static void main(String[] args) throws Exception{
FileInputStream input_document = new FileInputStream(new File("test_document.doc"));
WordExtractor my_word=new WordExtractor(input_document);
input_document.close();
Scanner document_scanner = new Scanner(my_word.getText());
Pattern words = Pattern.compile("(search)");
String key;
int count=0;
while (document_scanner.hasNextLine()) {
key = document_scanner.findInLine(words);
while (key != null) {
document_scanner.next();
count ++;
key = document_scanner.findInLine(words);
}
document_scanner.nextLine();
}
document_scanner.close();
System.out.println("Found Input "+ count + " times");
}
}
I tried this example on my word document and it printed the matching count accurately back on the screen. Give a try, and let us know if you are stuck. Note that to compile this program, would need poi-3.8.jar
or equivalent version.You also require poi-scratchpad-3.8.jar
. You can download both these from Apache POI distribution.
Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
HashMap<Key, Value> provides the basic implementation of the Map interface of Java and import java.util.HashMap package or its superclass. HashMap stores the data in (Key, Value) pairs, and accessed by an index of another type (e.g. an Integer). One object is used as a key to another object. If the duplicate key is inserted, it will replace the element of the corresponding key.
Approach :
- Declare a HashMap in Java of <String, Integer>
- Split the given string and store the words into a String array.
- Traversing the array, check if the word is in the HashMap or not.
- If it is not in the HashMap, then store the word as key and 1 as the initial value; if the word is present in the HashMap then increase the value against the word.
- Once the traversal is complete, print the HashMap.
Example:
Java
import
java.io.*;
import
java.util.HashMap;
import
java.util.Map;
class
GFG {
public
static
void
main(String[] args)
{
String str =
"Alice is girl and Bob is boy"
;
Map<String, Integer> hashMap =
new
HashMap<>();
String[] words = str.split(
" "
);
for
(String word : words) {
Integer integer = hashMap.get(word);
if
(integer ==
null
)
hashMap.put(word,
1
);
else
{
hashMap.put(word, integer +
1
);
}
}
System.out.println(hashMap);
}
}
Output
{Bob=1, Alice=1, and=1, is=2, girl=1, boy=1}
Note that in the above code, we have used Wrapper class i.e. Integer, since the get(key) method returns null.
You can also eliminate the use of integer variable from the above program.
Let us see the code below :
Java
import
java.io.*;
import
java.util.HashMap;
import
java.util.Map;
class
GFG {
public
static
void
main(String[] args)
{
String str =
"Alice is girl and Bob is boy"
;
Map<String, Integer> hashMap =
new
HashMap<>();
String[] words = str.split(
" "
);
for
(String word : words) {
if
(hashMap.containsKey(word))
hashMap.put(word, hashMap.get(word) +
1
);
else
hashMap.put(word,
1
);
}
System.out.println(hashMap);
}
}
Output
{Bob=1, Alice=1, and=1, is=2, girl=1, boy=1}
Like Article
Save Article
In my last post I have given an idea about How to change font of JLabel.
Here, I have shared Reading a text file and search any specific word from that.
Sometimes we may need to search any specific word from a text file. For this, we need to know how to read a text file using java.
Here I have given a simple way, just read a text file and find out the search keyword, after finding out the search key word it will be shown on which line the searched keyword was found.
** It will give you just a basic idea.
Click Here to download the Code+Reader1.txt to test yourself.
Code Example:
import java.io.*;
import java.util.*;
class filereader
{
public static void main(String args[]) throws Exception
{
int tokencount;
FileReader fr=new FileReader(“reader1.txt”);
BufferedReader br=new BufferedReader(fr);
String s;
int linecount=0;
String line;
String words[]=new String[500];
while ((s=br.readLine())!=null)
{
linecount++;
int indexfound=s.indexOf(“see”);
if (indexfound>-1)
{
System.out.println(“n”);
System.out.println(“Word was found at position::” +indexfound+ “::on line”+linecount);
System.out.println(“n”);
line=s;
System.out.println(line);
int idx=0;
StringTokenizer st= new StringTokenizer(line);
tokencount= st.countTokens();
System.out.println(“n”);
System.out.println(“Number of tokens found” +tokencount); System.out.println(“n”);
for (idx=0;idx<tokencount;idx++) {
words[idx]=st.nextToken();
System.out.println(words[idx]);
}
}
}
fr.close();
}
}
OUTPUT: