-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFileLetterCounter.java
More file actions
69 lines (52 loc) · 1.9 KB
/
Copy pathFileLetterCounter.java
File metadata and controls
69 lines (52 loc) · 1.9 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
import java.io.*;
/**@author Lemuel M. Uhuru
* @Date June 26, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 267 Challenge 6: File Letter Counter
*
* @Description Write a program that asks the user to enter the name of a file,
* 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 file. Use Notepad or
* another text editor to create a simple file that can be used to test the program.
*/
public class FileLetterCounter {
public static void main(String[] args) throws IOException{
// Create Scanner object for user input
Scanner input = new Scanner(System.in);
// Prompt user for file name
System.out.print("Enter file name: ");
String fileName = input.nextLine();
// Create File
File file = new File(fileName + ".txt");
// Ensure that file exist
if(!file.exists()){
System.out.println("The file " + fileName + ".txt does not exist.");
System.exit(0);
}
// Create scanner object to open file.
Scanner fileInput = new Scanner(file);
// Prompt user for the specified character and store in variable
System.out.println("Enter a single character: ");
String charInput = input.nextLine();
char character = charInput.charAt(0);
// Declare var to store lines from the file
String line;
// Declare counting variable
int count = 0;
// Construct while and for loop to calculate the number of
// times the specified character appears within the file.
while(fileInput.hasNext()){
line = fileInput.nextLine();
for(int j=0; j<line.length(); j++){
if(line.charAt(j)==character){
count += 1;
}
}
}
// Diplay character count
System.out.println("The character " + character + " appears in the file " + count + " times.");
// Close file
fileInput.close();
}
}