Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hashmaps, string buffers, and tokenizers

Status
Not open for further replies.

jin

Programmer
Aug 2, 2001
3
0
0
SG
Hi,

I have a short project...basically, I have a HashMap with several words in it. I also have a input html file with some text in it.

My project is that I have to make a new file...which has the same contents has the input file, BUT if any of the words from the Map shows up in the inputfile, I have to make those words hyperlinks.

For example, if the word "computer" is in the hashmap...and "The computer has 128 megs of RAM" is in the input.html file...i would create a new html file that says "The computer has 128 megs of RAM" -- BUT here 'computer' would be a hyperlink. (Thus, in the end, I would have a new HTML file which would have links to all words in the hashmap.)

I realllllllllllly need help here. I bet to many of you this is a simple problem!

( In the hashmap the key is the word, and the value is the hyperlink code. )

-Jin
jinen@hiphopsite.com
 

i don't even know if i have to use string buffer/string tokenizers....someone told me i should --> but i don't know how!

thanks again

-jin
 
Hi jin,

Smells like a homework ;)
Well, ok, here is somekind of example, it's not implemented the best possible way and it's case-sensitive... and it doesn't recognize words like <TAG>word. You have to figure those out by yourself :)

But here is an example:
_______________________________________________________
import java.util.*;
import java.io.*;

public class HttpLinks
{
public static void main(String[] argv)
{
new HttpLinks();
}

public HttpLinks()
{
_links = new HashMap(); // HashMap containing link names and destination address
_links.put(&quot;computer&quot;, &quot;
try
{
BufferedReader br = new BufferedReader(new FileReader(&quot;source.html&quot;)); // Open source doc

String line = null;
// StringBuffer is used because its much faster to use than 'String s1 += String s2'
StringBuffer source = new StringBuffer();

while((line = br.readLine()) != null) // Read document to StringBuffer
{
source.append(line);
}
checkLinks(source.toString());

}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

/**
* Check if source html document contains links defined in HashMap.
*/
private void checkLinks(String src_)
{
StringTokenizer st = new StringTokenizer(src_, &quot; &quot;); // 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(_links.containsKey(token)) // Token found in HashMap, add link
{
String linkDest = (String)_links.get(token);
dest.append(&quot;<a href=\&quot;&quot;).append(linkDest).append(&quot;\&quot;>&quot;).append(token).append(&quot;</a> &quot;);
}
else
{
dest.append(token).append(&quot; &quot;);
}
}
save(dest.toString()); // Save to disk
}

/**
* Save ready html document
*/
private void save(String dest_)
{
try
{
PrintWriter pw = new PrintWriter(new FileWriter(&quot;dest.html&quot;));
pw.println(dest_);
pw.flush();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

private HashMap _links;
}
______________________________________________________

I tested this only once and had a very simle html-document, source.html:
<HTML>
<BODY>
The computer has 128 megs of RAM
</BODY>
</HTML>

So it should work somehow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top