java - Counting characters method exception error? -


this question has answer here:

i have copied code answer on website (counting characters in string , returning count) , have amended suit own needs. however, seem getting exception error in method.

i appreciate here.

please forgive errors in code still learning java.

here code:

public class countthechars {  public static void main(string[] args){      string s = "brother drinks brandy.";      int countr = 0;      system.out.println(count(s, countr));  }   public static int count(string s, int countr){      char r = 0;      for(int = 0; i<s.length(); i++){          if(s.charat(i) == r){              countr++;          }          return countr;      }  }  } 

here exception:

exception in thread "main" java.lang.error: unresolved compilation problem:  method count(string) in type countthechars not applicable arguments (int)  @ countthechars.main(countthechars.java:12) 

you missing return statement in method public static int count(string s, int countr). not return int if s.length() == 0.

this should work expected:

public class countthechars {      public static void main(string[] args) {          string s = "brother drinks brandy.";          int countr = 0;          system.out.println(count(s, countr));      }      public static int count(string s, int countr) {          (int = 0; < s.length(); i++) {              if (s.charat(i) == 'r') {                  countr++;              }          }          return countr;      }  } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -