-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScannerDemo.java
More file actions
34 lines (30 loc) · 901 Bytes
/
Copy pathScannerDemo.java
File metadata and controls
34 lines (30 loc) · 901 Bytes
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
import java.util.Scanner;
public class ScannerDemo{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(true){
System.out.printf("%nMenu Options%n%n");
System.out.println("1: Frequency Count");
System.out.printf("2: Quit%n%n");
System.out.print("Enter your selection (1 or 2): ");
int selection = scanner.nextInt();
scanner.nextLine();
if(selection == 1){
System.out.printf("%nEnter sentence: ");
String sentence = scanner.nextLine();
System.out.print("Enter index: ");
int index = scanner.nextInt();
int count = 0;
for(int i=0;i<sentence.length();i++){
if(sentence.charAt(i) == sentence.charAt(index)){
count++;
}
}
System.out.printf("Count of [%c] in [%s]: %d%n", sentence.charAt(index), sentence, count);
}else if(selection == 2){
break;
}
}
scanner.close();
}
}