-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTask7.java
More file actions
30 lines (19 loc) · 896 Bytes
/
Copy pathStringTask7.java
File metadata and controls
30 lines (19 loc) · 896 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
import java.util.Scanner;
public class StringTask7 {
public class CharacterSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a line of text: ");
String enteredText = scanner.nextLine();
System.out.print("Enter a character to search for: ");
char searchCharacter = scanner.nextLine().charAt(0);
int numberOfOccurrences = 0;
int currentPosition = enteredText.indexOf(searchCharacter);
while (currentPosition != -1) {
numberOfOccurrences++;
currentPosition = enteredText.indexOf(searchCharacter, currentPosition + 1);
}
System.out.println("The character '" + searchCharacter + "' appears " + numberOfOccurrences + " time(s) in the text.");
}
}
}