如何计算文本文件中的字符“A”或“a”

时间:2017-03-12 22:27:27

标签: java

我需要创建一个程序,该程序将读取.TXT文件的内容,并输出文件中存在的As'(A或a)的数量。

  

任务:首先下载7B_Batch.txt文件并将其导入项目。创建一个程序,该程序将读入此文件的内容并输出文件中存在的As'(A或A)的数量。文件中总共有250行。

该文件包含以下字母:

X
j
9
p
Q
0
n
v
[etc...]

到目前为止,我的代码一直是:

import java.io.*;
import java.util.*;
public class lettercount {

    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader ("7B_Batch.txt"); 
         //connecting to file (7aname) by adding a file reader 
         BufferedReader br = new BufferedReader (fr); 
         //adding buffered reader which connects to the File Reader
         int total = 0;
         String line = br.readLine();
         char find = 'A';
         for ( int i = 0; i < 250; i++)
                 {
                    line = br.readLine();
                    if (line.equals(find))
                            {
                                total = total+1;
                            }
                 }
         System.out.println("Counting the As' in the file....");
         System.out.println("I found "+total +" As' in the file!");
    }

}

问题是,if (line.equals(find))行会引发NullPointerException

  

在lettercount.main(lettercount.java:16)的线程“main”java.lang.NullPointerException中的异常

3 个答案:

答案 0 :(得分:1)

您在循环之前使用br.readLine(),将其分配给变量line。但是你不能使用该值,因为你在循环开始时覆盖了这个值。

这样,您尝试读取文件的行251次,尽管循环运行250次。尝试阅读第251行时,文件中没有,br.readLine()返回null

在声明变量br.readLine()时删除对line的调用。

另一项改进是将for - 循环替换为while循环,直到br.readLine()返回null为止。这样,您就不必事先知道文件中有多少行。

答案 1 :(得分:0)

  • 您肯定需要检查来自缓冲读卡器的line是否为空。
  • 我认为你有一个额外的阅读,你不需要 - 在你定义变量的循环之前。这可能会导致循环内的NPE。
  • 我认为检查String是否等于char是没有多大意义的,因为结果总是假的。
  • 根据任务,您必须同时检查&#39; a&#39;和&#39; A&#39;。

以下是我用java 8流解决这个问题的方法,只需用FileInputStream替换ByteArrayInputStream或就地创建FileReader。

    ByteArrayInputStream in = new ByteArrayInputStream("a\nA\nb\nc\na\nd\n".getBytes());
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
        long count = reader.lines()
                .filter(letter -> letter.compareToIgnoreCase("a") == 0)
                .count();
        System.out.println("There are " + count + " a's in the file");
    }

希望,这有帮助。

答案 2 :(得分:0)

为了避免null,br.redLine()会在没有更多单词的情况下获得null,所以请使用一段时间,为了计算你需要的东西你可以使用Regex,看看这段代码我希望它可以帮助你(如果你使用Java 7代码会更简单),我使用了一个计数器int所以我知道manny aA在文件中是什么

private static final String FILENAME = "/Users/jucepho/Desktop/ansi.txt";

   public static void main(String[] args) {
            BufferedReader br = null;
            FileReader fr = null;
             String pattern = "[aA]";
              Pattern r = Pattern.compile(pattern);
              int counter=0;
            try {
                fr = new FileReader(FILENAME);
                br = new BufferedReader(fr);
                String sCurrentLine;
                br = new BufferedReader(new FileReader(FILENAME));
                while ((sCurrentLine = br.readLine()) != null) {
                    //System.out.println(sCurrentLine);
                    Matcher m = r.matcher(sCurrentLine);
                     if (m.find( )) {
                         System.out.println(m.group());
                         counter++;
                     }

                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)
                        br.close();
                    if (fr != null)
                        fr.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            System.out.println("There are :"+counter +" words");
        }

它给了我结果:

有:218字