Note that there are some explanatory texts on larger screens.

plurals
  1. POOutputting a Java map and mixing in HTML
    primarykey
    data
    text
    <p>I am currently working on my own version of a glossary written in Java. Truthfully, this is of academic nature and I was hoping someone could point me in the first direction. Anyway, I am reading in text from a text file and putting the words and their corresponding definitions into a Map (Tree Map to be more specific). Everything works good from there. Everything is in the map as it should be.</p> <p>Now I start to get to the part where I want to go into HTML and output the contents of the map. I know how to do that with iterators and that wasn't much of a problem. However, when I try to display the content mixed in with HTML I don't get all that I want. The page is ultimately supposed to look like this: <a href="http://cse.osu.edu/~weide/rsrg/sce/now/321/421/labs/lab10/glossary.html#book" rel="nofollow">http://cse.osu.edu/~weide/rsrg/sce/now/321/421/labs/lab10/glossary.html#book</a></p> <p>And there is this particularly tricky part where if there's a term contained within a definition it should be clickable. Here is what I have so far. Again, if anyone could help me figure out why the main guts of the HTML aren't displaying I would appreciate it very much! By the way, the text file I'm getting things from is called: terms.txt, and the html file writing to is called glossary.html.</p> <p>This is what I have so far:</p> <pre><code>public class Glossary { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Map&lt;String, String&gt; dictionary = new TreeMap&lt;String, String&gt;(); File htmlFile = new File( "/Users/myname/Documents/workspace/Lab10/src/glossary.html"); File file = new File( "/Users/myname/Documents/workspace/Lab10/src/terms.txt"); Writer out = new OutputStreamWriter(new FileOutputStream(htmlFile)); String term = null; String def = null; String key = null, value = null; String lead = null; String multiFinalDef = null; Set&lt;String&gt; checkValues = new HashSet&lt;String&gt;(); String leftOver = null; boolean check = false; Scanner input = null; try { input = new Scanner(file); while (input.hasNext()) { String keepTrack; boolean multi = false; String line = input.nextLine(); term = line; def = input.nextLine(); keepTrack = def; while (def.length() &gt; 0 &amp;&amp; input.hasNext()) { def = input.nextLine(); if (def.length() &gt; 0) { multiFinalDef = " " + keepTrack + def; multi = true; } } if (multi) { dictionary.put(term, multiFinalDef); } else { dictionary.put(term, keepTrack); } checkValues.add(term); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { out.write("&lt;HTML&gt;\n"); out.write("&lt;HEAD&gt;\n"); out.write("&lt;/HEAD&gt;\n"); out.write("&lt;BODY&gt;\n"); out.write("&lt;H1&gt;Glossary&lt;/H1&gt;\n"); out.write("&lt;HR /\n"); out.write("&lt;H2&gt;Index&lt;/H2&gt;\n"); out.write("&lt;UL&gt;\n"); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Set s = dictionary.entrySet(); Iterator iterator = s.iterator(); while (iterator.hasNext()) { Map.Entry m = (Map.Entry) iterator.next(); // getKey is used to get key of map. key = (String) m.getKey(); // getValue is used to get the value of the key in map. value = (String) m.getValue(); // this is just so I know the output from the map is actually correct. And indeed it is. System.out.println("Key:\t\t\tValue\n " + key + "\t\t\t " + value + "\n"); try { out.write("&lt;LI&gt;&lt;A HREF=\"#" + key + "\"&gt;" + key + "&lt;/A&gt;&lt;/LI&gt;\n"); out.write("&lt;/UL&gt;\n"); out.write("&lt;HR /&gt;\n"); } catch (IOException e) { e.printStackTrace(); } } out.write("&lt;H2&gt;Terms and Definitions&lt;/H2&gt;\n"); out.write("&lt;UL&gt;\n" + "&lt;P&gt;\n"); iterator = s.iterator(); while (iterator.hasNext()) { Map.Entry temp = (Map.Entry) iterator.next(); // getKey is used to get key of map. String keyTwo = (String) temp.getKey(); // getValue is used to get the value of the key in map. String valueTwo = (String) temp.getValue(); out.write("&lt;H3&gt;&lt;A NAME=\" " + keyTwo + "/&gt;&lt;B&gt;&lt;I&gt;&lt;FONT COLOR=\"red\"&gt;" + keyTwo + "&lt;/FONT&gt;&lt;/I&gt;&lt;/B&gt;&lt;/LI&gt;&lt;/H3&gt;\n"); for(String getTerm : checkValues){ if (valueTwo.contains(getTerm)) { check = true; int foundTermPosition = valueTwo.indexOf(getTerm); lead = valueTwo.substring(0, foundTermPosition - 1); //fix left over.. leftOver = valueTwo.substring(foundTermPosition, valueTwo.length()); out.write(lead); out.write("&lt;A HREF=\"#" + keyTwo + "\"&gt;" + keyTwo + "&lt;/A&gt;"); out.write(leftOver + "\n"); //out.write("&lt;/blockquote&gt;\n"); } } if( check == false) { out.write(lead + " " + valueTwo); } } //System.out.println(valueTwo + leftOver); // used to put words defined in file mentioned in definition // with hyperlinks to their associated locations, and output the // definition. out.write("&lt;/P&gt;\n" + "&lt;/UL&gt;\n"); out.write("&lt;/BODY&gt;\n"); out.write("&lt;/HTML&gt;"); out.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload