在文本文件中加粗字符串的过程

时间:2015-03-05 16:03:32

标签: java bufferedreader bufferedwriter

所以,我正在开发一个程序,其中包含一个名为orders的txt文件的条目,该文件指定要加粗的单词数和必须加粗的单词。我已经成功地用了一个词,但是当我尝试用两个单词时,输出变得加倍。例如:

Input:
2
Ophelia
him

Output:

ACT I
ACT I


SCENE I. Elsinore. A platform before the castle.
SCENE I. Elsinore. A platform before the castle.


FRANCISCO at his post. Enter to him BERNARDO 
FRANCISCO at his post. Enter to *him* BERNARDO 

这是我的代码,任何人都可以帮助我吗? PS:我猜错了布尔值。

static void bold(char bold, BufferedReader orders, BufferedReader in, BufferedWriter out) throws IOException
{
    String linha = in.readLine();
    boolean encontrou = false;
    String[] palavras = new String[Integer.parseInt(orders.readLine())];
    for (int i = 0; i < palavras.length; i++)
    {
        palavras[i] = orders.readLine();
    }

    while (linha != null)
    {
        StringBuilder str = new StringBuilder(linha);
        for (int i = 0; i < palavras.length && !encontrou; i++)
        {

            if (linha.toLowerCase().indexOf(palavras[i]) != -1)
            {
                str.insert((linha.toLowerCase().indexOf(palavras[i])), bold);
                str.insert((linha.toLowerCase().indexOf(palavras[i])) + palavras[i].length() + 1, bold);
                out.write(str.toString());
                out.newLine();

            }
            else
            {
                out.write(linha);
                out.newLine();
            }
        }
        linha = in.readLine();
    }

}

2 个答案:

答案 0 :(得分:0)

它会写出两次,因为每次迭代时都会为行(out.write(str.toString()))输出StringBuilder(linha),这至少是查找列表中的单词数。

out.write()语句移到循环之外,你应该没问题。

注意 这只会在每个单词的每一行中找到一个匹配项。如果您需要找到多个,代码会更复杂一些。您需要引入while循环而不是if测试才能进行匹配,或者您可以考虑使用基于您的单词palavras[i]的正则表达式replaceAll()。确保你尊重原件的大写并不简单,但可能。


修正版

static void bold(char bold, BufferedReader orders, BufferedReader in, BufferedWriter out) 
        throws IOException
{
    String linha = in.readLine();
    boolean encontrou = false;
    String[] palavras = new String[Integer.parseInt(orders.readLine())];
    for (int i = 0; i < palavras.length; i++)
    {
        palavras[i] = orders.readLine();
    }

    while (linha != null)
    {
        StringBuilder str = new StringBuilder(linha);
        for (int i = 0; i < palavras.length && !encontrou; i++)
        {

            if (linha.toLowerCase().indexOf(palavras[i]) != -1)
            {
                str.insert((linha.toLowerCase().indexOf(palavras[i])), bold);
                str.insert(
                        (linha.toLowerCase().indexOf(palavras[i])) + palavras[i].length() + 1, 
                        bold);
            }
        }
        out.write(str.toString());
        out.newLine();
        linha = in.readLine();
    }

}

使用replaceAll

static void bold(char bold, BufferedReader orders, BufferedReader in, BufferedWriter out) 
        throws IOException
{
    String linha = in.readLine();
    boolean encontrou = false;
    String[] palavras = new String[Integer.parseInt(orders.readLine())];
    for (int i = 0; i < palavras.length; i++)
    {
        palavras[i] = orders.readLine();
    }

    while (linha != null)
    {
        for (int i = 0; i < palavras.length && !encontrou; i++)
        {
            String regEx = "\\b("+palavras[i]+")\\b";
            linha = linha.replaceAll(regEx, bold + "$1"+bold);
        }
        out.write(linha);
        our.newLine();
        linha = in.readLine();
    }

}

P.S。 我已将找到的布尔值(encontrou)保留在其中,尽管此刻它没有做任何事情。

答案 1 :(得分:0)

这需要正则表达式替换WORD-BOUNDARY + ALTERNATIVES + WORD-BOUNDARY。

String linha = in.readLine(); // Read number of words to be bolded.
String[] palavras = new String[Integer.parseInt(orders.readLine())];
for(int i = 0; i < palavras.length; i++){
    palavras[i]=orders.readLine();
}

// We make a regular expression Pattern.
// Like "\\b(him|her|it)\\b" where \\b is a word-boundary.
// This prevents mangling "shimmer".
StringBuilder regex = new StringBuilder("\\b(");
for (int i = 0; i < palavras.length; i++) {
    if (i != 0) {
        regex.append('|');
    }
    regex.append(Pattern.quote(palavras[i]));
}
regex.append(")\\b");
Pattern pattern = Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE);

boolean encontrou = false;
linha = in.readLine(); // Read first line.
while(linha != null){
    Matcher m = pattern.matcher(linha);
    String linha2 = m.replaceAll(pattern, "*$1*");
    if (linha2 != linha) {
        encontrou = true; // Found a replacement.
    }
    out.write(linha2);
    out.newLine();
    linha = in.readLine(); // Read next line.
}

replaceAll(而不是replaceFirst)然后替换所有次出现。