字符串标记符 - 通过标记获取问题和答案

时间:2014-02-24 17:00:34

标签: java eclipse stringtokenizer

我删除了标签,因此arrayList看起来像这样:

Qstore.add(new QuestionObject("Question 2?", "1", "2", "3", "4", "B", "Starts with M", "Easy", "Sport"));
Qstore.add(new QuestionObject("Question 3?", "221", "233", "443", "455", "C", "Starts with M", "Easy", "Sport"));

我有我的Millionaire程序与ArrayList一起运行但是当GUI显示问题和答案时,它将问题和答案列表与其他arraylist混合

2 个答案:

答案 0 :(得分:0)

对于您的简单案例,您可以创建一个小型代码表(String[] tab = {"A","B","C","D","ANS"},然后查看一行中存在哪个代码(if (line.trim().startsWith("<"+tag[i]+">") ...

然后,您可以在开始标记结束的位置"<"+tag[i]+">"和结束标记开始的位置之间提取文本"</"+tag[i]+">"

答案 1 :(得分:0)

    BufferedReader in;
    String pattern = ".*<Question>(.+?)</Question>.*";

    in = new BufferedReader(new FileReader("fileName.txt"));

    String line = null;
    while ((line = in.readLine()) != null) {

        if (line.matches(pattern)) {
            String replacedText = line.replaceAll(pattern, "$1");
            System.out.println("replacedText: " + replacedText);
        }
    }

上面给出的代码使用正则表达式来获取标记中存在的问题