0

I have copied this code from a good answer on this website (counting characters in a String and returning the count) and have slightly amended it to suit my own needs. However, I seem to be getting an exception error in my method.

I would appreciate any help here.

Please forgive any errors in my code as I am still learning Java.

Here is my 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 i = 0; i<s.length(); i++){

        if(s.charAt(i) == r){

            countR++;

        }

        return countR;

    }

}

}

Here is the exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method count(String) in the type CountTheChars is not applicable for the arguments (int)

at CountTheChars.main(CountTheChars.java:12)
PrimalScientist
  • 831
  • 5
  • 17
  • 27

3 Answers3

1

You are missing a return statement in your method public static int count(String s, int countR). Currently it does not return an int if s.length() == 0.

This should work as 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) {

        for (int i = 0; i < s.length(); i++) {

            if (s.charAt(i) == 'r') {

                countR++;

            }

        }

        return countR;

    }

}
Matthias Herlitzius
  • 3,365
  • 2
  • 20
  • 20
1

2 issues:

  1. Your count method when doing the comparison of s.charAt(i) is comparing each letter in the word s to a variable called r that you made which you set as 0. Which means, technically your method is cOunting the number of times the number 0 occurs in your sentence. That is why you are getting 0. To fix that, remove your r variable and in your comparison, make s.charAt(i) == 'r' as the comparison. Take note of the apostrophe around the r Rio mean that you specifically refer to the character r.

  2. Your count method is not properly returning for cases where the string is nothing which means it will have a length of zero which means you're for loop is not run and your method will skip that as well as the return statement you have in there. To fix this issue move the return statement at the very bottom of the method so regardless of what string you get in, the return statement will always return (as it should because your method is expecting an int to be returned)

micnguyen
  • 1,419
  • 18
  • 25
0

Why can not you use s.length() to count number of characters (i.e length of the string) in String s?

EDIT: changed to incorporate the "count number of occurances of char r". You call the function as count(s,countR, r) and declare char r = 'a' or whatever char you want in main

public static int count(String s, int countR, char r){

    countR= 0;
    for(int i = 0; i<s.length(); i++){

        if(s.charAt(i) == r){

            countR++;

        }

        return countR;

    }

}
Bill
  • 5,263
  • 6
  • 35
  • 50