我这里有一个程序,它输入一个段落并将其写入文件。之后,它应计算每个字母的出现次数(区分大小写)。但是,它不计算字母出现次数。我想我把 for
循环放在了错误的地方。
import java.io.*;
import java.util.*;
public class Exercise1 {
public static int countLetters (String line, char alphabet) {
int count = 0;
for (int i = 0; i <= line.length()-1; i++) {
if (line.charAt(i) == alphabet)
count++;
}
return count;
}
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in));
PrintWriter outputStream = null;
Scanner input = new Scanner (System.in);
int total;
try {
outputStream = new PrintWriter (new FileOutputStream ("par.txt"));
System.out.println("How many lines are there in the paragraph you'll enter?");
int lines = input.nextInt();
System.out.println("Enter the paragraph: ");
String paragraph = buffer.readLine();
outputStream.println(paragraph);
int j;
for (j = 1; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}
outputStream.close();
System.out.println("The paragraph is written to par.txt");
for (int k=1; k<lines; k++) {
paragraph = buffer.readLine();
total = countLetters (paragraph, 'A');
if (total != 0)
System.out.println("A: "+total);
//I'll do bruteforce here up to lowercase z
}
}
catch(FileNotFoundException e) {
System.out.println("Error opening the file par.txt");
}
}
}
请帮我修复代码。我是编程新手,我需要帮助。非常感谢你!
答案 0 :(得分:6)
首先,你的初始阅读用户输入有点浪费,因为你读了一次然后进入for循环的其余部分 - 这不是问题,只是一个更好的代码。
// your code
String paragraph = buffer.readLine();
outputStream.println(paragraph);
int j;
for (j = 1; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}
你可以把它们放在循环中:
// better code
String paragraph;
int j;
for (j = 0; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}
然后你的第一个问题来自你读行的方式:
// your code - not working
outputStream.close();
for (int k=1; k<lines; k++) {
paragraph = buffer.readLine();
total = countLetters (paragraph, 'A');
考虑上面发生的事情:
paragraph = buffer.readLine();
- 此代码的作用是什么?它等待另一个用户输入(而不是读取已插入的内容)要解决上述问题:您需要阅读已经已写入的内容 - 而不是要求其他输入。然后,您可以将它们放入列表并编写for循环,而不是逐个强制逐个强制每个字符。
现在,您想要读取您已经创建的现有文件(即读取用户输入的内容):
BufferedReader fileReader = new BufferedReader(new FileReader(new File("par.txt")));
String allCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String aLineInFile;
// Read the file that was written earlier (whose content comes from user input)
// This while loop will go through line-by-line in the file
while((aLineInFile = fileReader.readLine()) != null)
{
// For every line in the file, count number of occurrences of characters
// This loop goes through every character (a-z and A-Z)
for(int i = 0; i < allCharacters.length(); i++)
{
// For each single character, check the number of occurrences in the current line
String charToLookAt = String.valueOf(allCharacters.charAt(i));
int numOfCharOccurancesInLine = countLetters (aLineInFile, charToLookAt);
System.out.println("For line: " + aLineInFile + ", Character: " + charToLookAt + " appears: " + numOfCharOccurancesInLine + " times " );
}
}
上面给出了每一行中每个字符的出现次数 - 现在你只需要对它们进行组织,以便跟踪整个文件的总数。
代码方面,可能有更好的方法来编写它以实现更清晰的实现,但上面的内容很容易理解(我只是很快就写了)。
答案 1 :(得分:0)
在一个循环中完成所有事情:
for (j = 1; j<lines; j++) {
paragraph = buffer.readLine();
total = countLetters (paragraph, 'A');
if (total != 0)
System.out.println("A: "+total);
outputStream.println(paragraph);
}
答案 2 :(得分:0)
您可以使用HashTable计算每个案例的致函:
final Pattern patt = Pattern.compile("A-Za-z]");
final HashMap<Character, Integer> tabChar = new HashMap<Character, Integer>(
52);
// replace : paragraph = buffer.readLine();
// Unless you use it outside, you can declare it 'final'
final char[] paragraph = "azera :;,\nApOUIQSaOOOF".toCharArray();
for (final Character c : paragraph ) {
if (Character.isLetter(c)) {
Integer tot = tabChar.get(c);
tabChar.put(c, (null == tot) ? 1 : ++tot);
}
}
输出:
{F=1, A=1, O=4, I=1, U=1, Q=1, S=1, e=1, a=3, r=1, p=1, z=1}
您可以使用final TreeSet<Character> ts = new TreeSet(tabChar.keySet());
对字符进行排序,然后使用get(c);
tabChar
对其进行排序
答案 3 :(得分:0)
之前的答案可以解决您的问题,但另一种避免暴力的方法可能是使用ASCII字符值循环。