java - 输出 Java map 并在 HTML 中混合

标签 java html dictionary

我目前正在开发我自己的用 Java 编写的术语表版本。说实话,这是学术性质的,我希望有人能指出我的第一个方向。无论如何,我正在从文本文件中读取文本,并将单词及其相应的定义放入映射(更具体地说是树映射)中。从那里一切都运转良好。一切都在 map 上,它应该是这样。

现在我开始进入我想要进入 HTML 并输出 map 内容的部分。我知道如何使用迭代器来做到这一点,这不是什么大问题。然而,当我尝试显示与 HTML 混合的内容时,我没有得到我想要的全部内容。该页面最终应该如下所示:http://cse.osu.edu/~weide/rsrg/sce/now/321/421/labs/lab10/glossary.html#book

还有一个特别棘手的部分,如果定义中包含术语,那么它应该是可点击的。这是我到目前为止所拥有的。再说一次,如果有人能帮助我弄清楚为什么 HTML 的主要内容没有显示,我将非常感激!顺便说一句,我从中获取信息的文本文件称为:terms.txt,写入的 html 文件称为lossary.html。

这是我到目前为止所拥有的:

public class Glossary {

/**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    Map<String, String> dictionary = new TreeMap<String, String>();

    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<String> checkValues = new HashSet<String>();
    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() > 0 && input.hasNext()) {
                def = input.nextLine();

                if (def.length() > 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("<HTML>\n");
        out.write("<HEAD>\n");
        out.write("</HEAD>\n");
        out.write("<BODY>\n");
        out.write("<H1>Glossary</H1>\n");
        out.write("<HR /\n");
        out.write("<H2>Index</H2>\n");
        out.write("<UL>\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("<LI><A HREF=\"#" + key + "\">" + key + "</A></LI>\n");
            out.write("</UL>\n");
            out.write("<HR />\n");

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

    }

    out.write("<H2>Terms and Definitions</H2>\n");
    out.write("<UL>\n" + "<P>\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("<H3><A NAME=\" " + keyTwo + "/><B><I><FONT COLOR=\"red\">"
                + keyTwo + "</FONT></I></B></LI></H3>\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("<A HREF=\"#" + keyTwo + "\">" + keyTwo + "</A>");
            out.write(leftOver + "\n");
            //out.write("</blockquote>\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("</P>\n" + "</UL>\n");
    out.write("</BODY>\n");
    out.write("</HTML>");

    out.close();
}

}

最佳答案

当你的程序达到时

out.write("<H2>Terms and Definitions</H2>\n");
out.write("<UL>\n" + "<P>\n");

while (iterator.hasNext()) {
   ...

迭代器没有剩余任何项目,因为在打印索引时,它在前几行的第一个 while 循环中耗尽了。要再次迭代 map ,您需要再次调用迭代器方法。所以上面的 block 将变成:

out.write("<H2>Terms and Definitions</H2>\n");
out.write("<UL>\n" + "<P>\n");

iterator = s.iterator();
while (iterator.hasNext()) {
   ...

关于java - 输出 Java map 并在 HTML 中混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13677765/

相关文章:

python - 迭代 pandas DataFrame 中的行并创建一个字典

java - 基类没有定义 equals 但子类需要定义。如何实现?

javascript - knockout.js 为 <select> 和 <option> 绑定(bind)不同的文本

javascript - 在 HTML 表单上使用 GET 作为方法不会附加到 URL 中吗?

html - 网页浏览中的常用键盘导航(访问键)?

javascript - 在 React render() 的 map() 函数中调用函数

python - 从 python 列表中获取字典

java - Hibernate:我们可以在双方都是拥有方的实体之间建立关系吗?

java - 如何键入字符串内容 :encoded = "Hello"; in java?

java - 如何在特定栏上书写?