StringTokenizers....

hey everybody,

i have a hashmap which has several words/phrases in it (each element is not neccesarily a single word..it might be two++). for example, lets pretend the term: "computer system" is in the HashMap.

i have to open an HTML file (which has some text in it) and everytime the phrase "computer system" turns up, i have to turn it into a hyperlink...for example, if the input.html file had:

'The computer system is very old.'

I would create a new file called updated.html that says

'The computer system is very old' --> however, computer system would be a hyperlink.

i've coded quite a bit, but i'm faced with a problem.

after storing the input.html document in a stringbuffer, i do the following:

private void checkLinks(String src_)

{

StringTokenizer st = new StringTokenizer(src_, " "); // Use space as delimiter

int tokens = st.countTokens(); // Number of tokens in document

StringBuffer dest = new StringBuffer(" "); // Temporary storage

for(int i=0; i<tokens; i++) // go through all tokens

{

String token = st.nextToken();

if(data.wordBank.containsKey(token)) // Token found in HashMap, add link

{

String linkDest = (String)data.wordBank.get(token);

dest.append("><a href=\"").append(linkDest).append("\">").append(token).append("</a> ");

}

else

{

dest.append(token).append(" ");

}

}

save(dest.toString()); // Save to disk

}

this code works well -- however, only if the terms in the HashMap are single words..."computer system" would not be detected. is there a good way i can modify my code to permit this?

any feedback is appreciated.

thanks,

-jin

[1817 byte] By [jtk9] at [2007-9-26 3:11:41]
# 1

Try using indexOf and subString.

Like this:

int x =src_.indexOf("computer system");

if ( x>-1)

{

// check if x>0

if (x>0)

String start = src_.subString(0, x-1);

else

start = "";

// check if x+the length of string is at end of String

if (x+"computer system".length() < src_.length()

String end = src_.subString(x+"computer system".length());

else

end="";

// build your href String here

String href = ...;

String newString = start+href+end;

}

WynEaston at 2007-6-29 11:19:22 > top of Java-index,Archived Forums,Java Programming...