-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathLetterCounter.java
More file actions
47 lines (37 loc) · 1.3 KB
/
Copy pathLetterCounter.java
File metadata and controls
47 lines (37 loc) · 1.3 KB
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
import java.util.Scanner;
/**@author Lemuel M. Uhuru
* @Date June 25, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 267 Challenge 5: Letter Counter
*
* @Description Write a program that asks the user to enter a string, and then asks
* the user to enter a character. The program should count and display the number of
* times that the specified character appears in the string.
*/
public class LetterCounter {
public static void main(String[] args) {
// Declare variable to store string
String text;
// Declare var to store character
char character;
// Create Scanner object
Scanner keyboard = new Scanner(System.in);
// Prompt the user for String input
System.out.println("Enter a string: ");
text = keyboard.nextLine();
// Prompt the user for Char input
System.out.println("Enter a character: ");
character = keyboard.nextLine().charAt(0);
// Declare counter variable
int count = 0;
// Construct for loop to calculate the number of
// times that the specified character appears in the string.
for(int i = 0; i < text.length(); i++){
if(text.charAt(i) == character){
count += 1;
}
}
// Diplay the number of times the character appears
System.out.println("The character appears " + count + " times");
}
}