java 计数字母出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15021163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting letter occurrence
提问by Kyle
The program needs to count and display the number of times that the specified charector appears in the text file.
程序需要统计并显示指定字符在文本文件中出现的次数。
Currently turning up zero for the total. I'm not if I should be using a different loop, I've also tried using a 'for' loop.
目前总数为零。我不知道我是否应该使用不同的循环,我也尝试过使用“for”循环。
// Hold user input and sum
String fileName; // Holds the name of the file
String letter; // Letter to search for in the file
int total = 0; // Holds the total number of characters in the file
// Get the name of the file and character from the user
fileName = JOptionPane.showInputDialog("Please enter the name of a file:");
letter = JOptionPane.showInputDialog("Please enter a letter contained in the string");
// Open the file for reading
File file = new File(fileName);
Scanner inputFile = new Scanner(file); // Declare new scanner object for file reading
// Set accumulator to zero
int count = 0;
if (inputFile.nextLine().equalsIgnoreCase(letter)) {
count++; // add letter occurrence
total += count; // add the letter occurrence to the total
}
回答by Sudhanshu Umalkar
BufferedReader reader = new BufferedReader(new FileReader("somefile.txt"));
int ch;
char charToSearch='a';
int counter=0;
while((ch=reader.read()) != -1) {
if(charToSearch == (char)ch) {
counter++;
}
};
reader.close();
System.out.println(counter);
Is this of any help?
这有什么帮助吗?
回答by JRR
There is bug in your code.Correct code below-
您的代码中存在错误。以下更正代码-
String fileName; // Holds the name of the file
String letter; // Letter to search for in the file
// Get the name of the file and character from the user
fileName = "C:\bin\GWT.txt";
letter = "X";
// Open the file for reading
File file = new File(fileName);
Scanner inputFile = new Scanner(file); // Declare new scanner object for file reading
// Set accumulator to zero
int count = 0;
while(inputFile.hasNext()) {
if (inputFile.nextLine().toLowerCase().contains(letter.toLowercase())) {
count++; // add letter occurrence
}
}
System.out.println(count);
回答by Achintya Jha
String line=""
while(inputFile.hasNext()) {
line = inputFile.nextLine();
for(int i=0; i<line.length(); i++ ){
if (line.charAt(i)== letter)
count++;
}
}
回答by cowls
This answer is assuming each line in your text file contains just one letter as your question suggests.
这个答案假设您的文本文件中的每一行只包含您的问题所建议的一个字母。
You need to wrap your if statement in a loop as currently you are only checking the first line of the file:
您需要将 if 语句包装在一个循环中,因为目前您只检查文件的第一行:
while(inputFile.hasNext()) {
if (inputFile.nextLine().equalsIgnoreCase(letter)) {
count++; // add letter occurrence
total += count; // add the letter occurrence to the total
}
}
Also you can replace:
你也可以替换:
count++;
total+= count;
with just
只是
total++;