java - Counting characters method exception error? -
this question has answer here:
- java: unresolved compilation problem 7 answers
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
Post a Comment