How do I call this method properly?

23 Views Asked by At

I'm sure this is a simple fix, but its giving me a headache. Why is count not able to be printed

Homework assignment so I can't change too much.

package practice.stuff.here;
import java.util.Scanner;


/**
 *
 * @author Danan
 */
public class PracticeStuffHere {

    public static void main(String[] args) {

        Scanner input= new Scanner(System.in);
        System.out.println("Give me a saying you use");
        String saying = input.nextLine();

        System.out.println("The saying you gave me has the character a in it " );

        System.out.println(count(saying, char a)); //////this is the issue im having 

    }

    public static int count(String saying, char a) {
        int countR = 0;

        for(int i = 0; i <saying.length(); i++)
        {
            if(saying.charAt(i) == a)
                countR++;
        }
       return countR;
    }                
}
1

There are 1 best solutions below

0
G1N On

The actual function call is correct, the issue you run into is the 2nd param you're passing in:

count(saying, char a)

char a in your main method doesn't actually refer to anything. You have to passed initialized variables into your method, so if you do something like:

char a = 'a';
System.out.println(count(saying, a));

you can pass in the actual character 'a' as the second param which is what I think you're trying to do. You can also just pass in the actual character 'a' directly:

System.out.println(count(saying, 'a'));