-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputCharCountSearch.java
56 lines (48 loc) · 1.8 KB
/
InputCharCountSearch.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class InputCharCountSearch {
int print(ArrayList<String> list, char chars) {
int count = 0;
// for (String str : list) {
// if (str.toLowerCase().contains(String.valueOf(chars))) {
// System.out.println(str);
// count++;
// }
// }
for (String str : list) {
for (int i = 0; i < str.length(); i++) {
if (str.toLowerCase().charAt(i) == chars) {
System.out.println(str);
count++;
}
}
}
// -------------------------------------------------------------------------------------
// for (String str : list) {
// if (str.toLowerCase().contains(String.valueOf(chars).toLowerCase())) {
// System.out.println(str);
// }
// char[] ch = str.toLowerCase().toCharArray();
// for (int j = 0; j < ch.length; j++) {
// if (chars == ch[j]) {
// count++;
// }
// }
// }
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>(Arrays.asList("Bharat", "Yash", "Aniket", "Kick"));
System.out.println("Enter any character to search in name : ");
char chars = sc.next().toLowerCase().charAt(0);
InputCharCountSearch charCountSearch = new InputCharCountSearch();
int intCount = charCountSearch.print(list, chars);
if (intCount == 0) {
System.out.println("Match not found!");
} else {
System.out.println("\nCount Character (" + chars + ") : " + intCount);
}
}
}