我很难将“%:”添加到发生替换的行中。该程序遍历整个字母表,并将所有小写字母转换为大写字母,将大写字母转换为小写字母。然后,在文件的末尾,它应该添加
"%: a: x:b: y....z: z"
其中x,y和z是每行发生的替换次数。我怎样才能将“%:”添加到具有替换的行?并且还要注意,如果有替换,每行只有一个“%:”。谢谢。
File file = new File("old.tex");
Scanner scanner = new Scanner(file);
PrintWriter writer = new PrintWriter("new.tex");
while(scanner.hasNextLine()){
int numberlc;
int numberuc;
String line = scanner.nextLine();
line=line+ "%:";
for(char ch='a';ch<='z';ch++){
numberlc=numberOccurances(line, ch);
char upperch=Character.toUpperCase(ch);
numberuc=numberOccurances(line, upperch);
for(int y=0; y<line.length(); y++){
if(line.charAt(y)==ch)
line=line.substring(0,y) + upperch + line.substring(y+1);
else if(line.charAt(y)==upperch)
line=line.substring(0,y) + ch + line.substring(y+1);
}
if(numberlc>0)
line=line + ch + " " + numberlc + ":";
if(numberuc>0)
line=line + upperch + " " + numberuc + ":";
}
writer.println(line);
}
writer.close()
答案 0 :(得分:0)
我建议您将这些添加内容分开,直到您完成该行。
这样的事情:
StringBuilder changes = new StringBuilder("%: ");
for(char ch='a';ch<='z';ch++){
numberlc=numberOccurances(line, ch);
char upperch=Character.toUpperCase(ch);
numberuc=numberOccurances(line, upperch);
for(int y=0; y<line.length(); y++){
if(line.charAt(y)==ch)
line=line.substring(0,y) + upperch + line.substring(y+1);
else if(line.charAt(y)==upperch)
line=line.substring(0,y) + ch + line.substring(y+1);
}
if(numberlc>0)
changes.append(ch + " " + numberlc + ":");
if(numberuc>0)
changes.append(upperch + " " + numberuc + ":");
}
if (changes.length() > 3)
line += changes.toString();
writer.println(line);
注意:此代码未经过测试