1

我这里有一个程序,它输入一个段落并将其写入一个文件。之后,它应该计算每个字母的出现次数(区分大小写)。但是,它不计算字母出现的次数。我想我把 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");
        }

    }

}

请帮我修复代码。我是编程新手,我需要帮助。非常感谢!

4

4 回答 4

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 循环,而不是一个一个地强制每个字符。

所以现在,您想从您已经创建的现有文件中读取(即读取用户输入的 WAS 内容):

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 " );
      }         
}

以上为您提供了每行中每个字符的出现次数 - 现在您只需组织它们以跟踪整个文件的总数。

在代码方面,可能有更好的方法来编写它以获得更清晰的实现,但上面的内容很容易理解(而且我写得很快)。

于 2012-07-18T14:53:38.987 回答
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);    
        }
于 2012-07-18T14:41:02.327 回答
0

以前的答案可以解决您的问题,但另一种避免蛮力的方法可能是使用使用 ASCII 字符值的循环。

于 2012-07-19T03:26:00.340 回答
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

于 2012-07-18T15:05:00.313 回答