I need to create a program that will read in the contents of a .TXT file and output how many As’ (either A or a) that are present within the file.
Task: Start by downloading and importing the
7B_Batch.txt
file into your project. Create a program that will read in the contents of this file and output how many As’ (either A or a) that are present within the file. There are a total of 250 lines in the file.
The file has these letters:
X
j
9
p
Q
0
n
v
[etc...]
And my code so far has been:
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!");
}
}
The issue is that the line if (line.equals(find))
throws a NullPointerException
:
Exception in thread "main" java.lang.NullPointerException at lettercount.main(lettercount.java:16)