forked from ngoyal16/java_program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountChar.java
37 lines (29 loc) · 1.03 KB
/
CountChar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;
public class CountChar{
static Scanner user_input = new Scanner( System.in );
static String str;
static char letter;
public static void main(String []args){
getInput(); // get the input from user
int count = 0;
for(int i=0; i<str.length(); i++){
if(str.charAt(i) == letter){
count++;
}
}
printResult(count); // print the result
}
public static void getInput(){
System.out.print("Please Enter String : ");
str = user_input.nextLine().toLowerCase();
System.out.print("Please Enter Charter you would like to fine : ");
letter = user_input.next().trim().toLowerCase().charAt(0);
}
public static void printResult(int result){
if(result != 0){
System.out.println("charater found " + result + " times");
}else{
System.out.println("String won't have the charter you looking for");
}
}
}