Java 在文本文件中使用粗体字符串的过程

Java 在文本文件中使用粗体字符串的过程,java,bufferedreader,bufferedwriter,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

所以,我正在做一个程序,它有一个名为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();
    }

}
static void bold(char bold、BufferedReader orders、BufferedReader in、BufferedWriter out)抛出IOException
{
字符串linha=in.readLine();
布尔值encontrou=false;
String[]palavras=新字符串[Integer.parseInt(orders.readLine())];
for(int i=0;i
它被写了两次,因为您每次迭代该行(
linha
)时都会输出StringBuilder(
out.write(str.toString())
),这至少是查找列表中的字数

移出。write()
语句移出循环,您应该会没事的

注意这将在每行中为每个单词找到一个匹配项。如果您需要找到多个,那么代码会稍微复杂一些。你需要引入<<代码>而<代码>循环,而不是你的代码>如果< /代码>测试匹配,或者你可以考虑使用基于你的Word <代码> Palavras[i] < /Cord>的正则表达式。确保您尊重原作的资本化并不简单,但这是可能的


固定版本
static void bold(char bold、BufferedReader orders、BufferedReader in、BufferedWriter out)
抛出IOException
{
字符串linha=in.readLine();
布尔值encontrou=false;
String[]palavras=新字符串[Integer.parseInt(orders.readLine())];
for(int i=0;i
全部替换
static void bold(char bold、BufferedReader orders、BufferedReader in、BufferedWriter out)
抛出IOException
{
字符串linha=in.readLine();
布尔值encontrou=false;
String[]palavras=新字符串[Integer.parseInt(orders.readLine())];
for(int i=0;i


p.S.我将找到的布尔值(
encontrou
)保留在中,尽管它目前没有做任何事情。

这需要用正则表达式替换单词边界+替代项+单词边界

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.
}
String linha=in.readLine();//阅读要加粗的字数。
String[]palavras=新字符串[Integer.parseInt(orders.readLine())];
for(int i=0;i

一个replaceAll(而不是replaceFirst)然后替换所有出现的内容。

编辑您的代码以添加整个代码和输入,这样您就可以获得更多的帮助。我将打开“是”的建议。建议是什么?谢谢Richard,顺便说一句,你能解释一下为什么它只会在第一次遇到的情况下加粗吗?哈-是的-我只是扩展了我的答案,以包括这一点,因为我注意到了这个问题/功能。我会更新我的答案来解决这个问题。
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();
    }

}
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.
}