2
Я не знаю, почему этот код не преобразовывает все экземпляры 'm' в 'M' и экземпляры 'M' в 'm'. Например, следует преобразовать в:Почему это не конвертирует все экземпляры?
Report 98-17, Faculty of Technical matheMatics and InforMatics,%:m 2:M 1:
, когда он преобразовывает в:
Report 98-17, Faculty of Technical MatheMatics and InforMatics,%:m 2:M1:
Спасибо.
public static int numberOccurances(String l, char f){
int count=0;
for(int x=0; x<l.length();x++){
if(l.charAt(x)==f)
count++;
}
return count;
}
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
File file = new File("Old.txt");
Scanner scanner = new Scanner(file);
PrintWriter writer = new PrintWriter("New.txt", "UTF-8");
while(scanner.hasNextLine()){
String line = scanner.nextLine();
int numberm=numberOccurances(line, 'm');
int numberM = numberOccurances(line, 'M');
for(int y=0; y<line.length(); y++){
if(line.charAt(y)=='M'){
line=line.substring(0,y) + 'm' + line.substring(y+1);
}
if(line.charAt(y)=='m'){
line=line.substring(0,y) + 'M' + line.substring(y+1);
}
}
if(numberm>0&&numberM>0)
line=line + "%:m " + numberm + ":M" + numberM + ":";
if(numberm>0&&numberM==0)
line=line + "%:m " + numberm + ":";
if(numberM>0&&numberm==0)
line=line + "%:M " + numberM + ":";
writer.println(line);
}
writer.close();
}