Created
May 4, 2013 05:56
-
-
Save jimmy087/5516388 to your computer and use it in GitHub Desktop.
checks how many times a character appears in a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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