Skip to content

Instantly share code, notes, and snippets.

@jimmy087
Created May 4, 2013 05:56
Show Gist options
  • Save jimmy087/5516388 to your computer and use it in GitHub Desktop.
Save jimmy087/5516388 to your computer and use it in GitHub Desktop.
checks how many times a character appears in a string.
import java.util.Scanner;
public class Exercise9_4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string: ");
String s1 = input.nextLine();
System.out.println("Please enter a character: ");
char s2 = input.nextLine().charAt(0);
int counts = counts(s1, s2);
System.out.println(counts);
}
public static int counts(String s1, char s2) {
// TODO Auto-generated method stub
int counts = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == s2) {
counts++;
}
}
return counts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment