hi
how i can do this by StringTokenizer

Is there a more easy efficient method?

Recommended Answers

All 8 Replies

hi
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, ",");
                while(tz.hasMoreTokens()){
                String next=tz.nextToken();
                    if((next).equals(jTextField1.getText())){
                    jEditorPane1.setForeground(Color.yellow);
                    }

                }
        }

        } catch (IOException ex) {
        }

        } catch (FileNotFoundException ex) {
        }

how i highlight the word and make it shown ?

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.

I am surprised that no one mentioned regular expressions.

No I did not because I was short on time and used the short, regex, instead, see my previous post. ;)

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: ");
		
		try 
		{
			String text = br.readLine();
			System.out.println("Enter the word to match : ");
			String word = br.readLine();
	
		
			String[] strarr = text.split(" ");
			
			for(String str:strarr)
			{
				if(str.equalsIgnoreCase(word))
					System.out.println(word + " exists in " + text);	
				
			}
			
		} 
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.