title: Why is this not converting all instances?
body: <p>I do not know why this code is not converting all instances of 'm' to 'M' and instances of 'M' to 'm'. For example, it should be converting to:</p>
  
  <pre><code>Report 98-17, Faculty of Technical matheMatics and InforMatics,%:m 2:M 1:
  </code></pre>
  
  <p>when it is converting to:</p>
  
  <pre><code>Report 98-17, Faculty of Technical MatheMatics and InforMatics,%:m 2:M1:
  </code></pre>
  
  <p>Thanks.</p>
  
  <pre><code>public static int numberOccurances(String l, char f){ 
      int count=0; 
      for(int x=0; x&lt;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&lt;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&gt;0&amp;&amp;numberM&gt;0)
          line=line + "%:m " + numberm + ":M" + numberM + ":";
      if(numberm&gt;0&amp;&amp;numberM==0)
          line=line + "%:m " + numberm + ":";
      if(numberM&gt;0&amp;&amp;numberm==0)
          line=line + "%:M " + numberM + ":";
      writer.println(line);
  }
      writer.close();
  }
  </code></pre>