0

我目前正在编写自己的用 Java 编写的词汇表版本。老实说,这是学术性质的,我希望有人能指出我的第一个方向。无论如何,我正在从文本文件中读取文本并将单词及其相应的定义放入 Map(更具体的 Tree Map)。从那里一切都很好。一切都在地图中,因为它应该是。

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

还有一个特别棘手的部分,如果定义中包含一个术语,它应该是可点击的。这是我到目前为止所拥有的。同样,如果有人可以帮助我弄清楚为什么 HTML 的主要内容没有显示,我将非常感激!顺便说一句,我从中获取内容的文本文件称为:terms.txt,写入的 html 文件称为glossary.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();
}

}
4

2 回答 2

1

当您的程序到达时

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

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

迭代器没有更多的项目,因为它在前几行的第一个 while 循环中耗尽,而您正在打印索引。要再次遍历地图,您需要再次调用迭代器方法。所以上面的块会变成:

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

iterator = s.iterator();
while (iterator.hasNext()) {
   ...
于 2012-12-03T09:55:11.393 回答
0

据我了解,您想生成 html 文档。以我的拙见,在您的案例中,最好和通用的方法 -使用任何模板引擎。例如 -阿帕奇速度

浏览本教程需要几分钟

于 2012-12-03T10:34:31.087 回答