- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCounter.java
More file actions
Latest commit
51 lines (42 loc) · 1.68 KB
/
Copy pathLetterCounter.java
File metadata and controls
51 lines (42 loc) · 1.68 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
48
49
50
51
//LetterCounter.java
importjava.util.Scanner; // Needed for the Scanner class
importjava.lang.Character; // Needed for the Character class
/**
This program will count the number of times a chosen character
appears in a given String
*/
publicclassLetterCounter
{
publicstaticvoidmain(String[] args)
{
StringuserString; // The string provided by the user
StringuserStringLower; // The userString lower cased
charuserLetter; // The letter the user would like counted
charuserLetterLower; // The userLetter lower cased
inti; // The characters of the string
intappearances = 0; // The number of appearances of the userLetter
// Create a Scanner
Scannerkeyboard = newScanner(System.in);
// Ask the user to enter a String
System.out.println("Please enter a word or write a sentance " +
"that you would like to have a letter " +
"counted for: ");
userString = keyboard.nextLine();
System.out.println();
// Ask the user to provide a letter
System.out.println("Please provide the letter you would like counted: ");
userLetter = keyboard.next().charAt(0);
System.out.println();
// Convert both inputs to lowercase
userStringLower = userString.toLowerCase();
userLetterLower = Character.toLowerCase(userLetter);
for (i = 0; i < userStringLower.length(); i++)
{
if (userStringLower.charAt(i) == userLetterLower)
{
appearances++;
}
}
System.out.println("The character " + userLetter + " appears " + appearances + " times.");
}
}