9 Years
Discussion Span
Last Post by AbhikGhosh

how i can do this by StringTokenizer

Is there a more easy efficient method?

Easy is relative, what might be easier for some could be really hard for you.
Google StringTokenizer examples, read the API and then start writing some code. When you have shown some effort come back, post your code and ask a specific question.

This tutorial should get you started.


this is my code :

    String line=null;

    try {
        BufferedReader br = new BufferedReader(new FileReader(jEditorPane1.getText()));
        try {

            while ((line = br.readLine()) != null) {
                StringTokenizer tz=new StringTokenizer(line, ",");
                String next=tz.nextToken();


        } catch (IOException ex) {

        } catch (FileNotFoundException ex) {

how i highlight the word and make it shown ?

Edited by mike_2000_17: Fixed formatting


Not saying that these data structures are the most efficient, but one thing you could do is read the entire file into a String (would be inefficient), then search by substring. Continuously increase the size of the substring until it either didn't match the word you're looking for or you found a match. If it didn't match the word (lets say you're looking for the word PROGRAM and it says PRA) you would stop there, move the part of the text you're looking at up a few characters, and start over.

I'm not sure what examples the other posters are pointing you towards, but they will probably be the easier route. This is what I'd do if I wasn't going to look for classes and methods that make my life easier.


What you could also do is read the file line by line, the same way as you are doing now and match a pattern in the read line using the String class' matches method. Where the pattern could simply be the string to be found in the file.

Note : The StringTokenizer is a legacy class and is use isn't recommended this is what the java guys have to say about it. StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. In your case, instead of the split method the matches would do the job, which itself uses regex.


Hi I have written this code and it runs successfully . Hope it helps.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchText 
	public static void main(String[] args)
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter Text: ");
			String text = br.readLine();
			System.out.println("Enter the word to match : ");
			String word = br.readLine();
			String[] strarr = text.split(" ");
			for(String str:strarr)
					System.out.println(word + " exists in " + text);	
		catch(IOException e)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.