- #1
ParticleGinger6
- 32
- 5
- Homework Statement
- Study the appropriate material on Blackboard about doing input and output in Java from the command line, and using files.
The problem: You are asked to write a text analyzer program that reads a text file and counts the number of lines, number of words (King’s counts as one word) and the frequency of occurrence of each of the 26 letters of the alphabet.
Note:
• We ignore case: Treat upper and lowercase letters to be the same.
• Assume that there is at least one space between words on the same line.
• Only characters A-Z are to be counted.
For example, if the text file contained this text shown below:
TextAnalyzerData.txt
Baa, baa, black sheep
Have you any wool?
Yes sir, yes sir
Three bags full.
One for my master
And one for the dame
One for the little boy
Who lives down the lane.
The Analyzer should print the following information:
Number of Lines: 8
Number of Words: 34
Letter Counts
Frequency of A: 12
Frequency of B: 5
Frequency of C: 1
Frequency of D: 3
Frequency of E: 18
Frequency of F: 4
Frequency of G: 1
Frequency of H: 7
Frequency of I: 4
Frequency of J: 0
Frequency of K: 1
Frequency of L: 8
Frequency of M: 3
Frequency of N: 7
Frequency of O: 12
Frequency of P: 1
Frequency of Q: 0
Frequency of R: 7
Frequency of S: 8
Frequency of T: 7
Frequency of U: 2
Frequency of V: 2
Frequency of W: 3
Frequency of X: 0
Frequency of Y: 6
Frequency of Z: 0
So how should you write this program?
To begin with, you need to write a Java class called “TextAnalyzer”. When thinking about writing this class, note that it must meet the following specifications:
• It should have the following instance variables (sometimes called attributes):
private int lineCount;
private int wordCount;
private int[] frequencies = new int[26];
• It should have “get” methods for each of the instance variables – e.g., it should have this method:
public int getLineCount()
{
return lineCount;
}
Note how we used case in the “get” method name given the case we used to create the instance variable name – i.e, the variable name was lineCount (using camel case as Java expects – note that the first lower case ‘l’ in the variable name), but the get method’s name begins with ‘get’, and is written as ‘getLineCount()’, where the lower case ‘l’ in the variable name gets transformed to an upper case ‘L’. You should use an identical style in all the get methods you write.
• It should have a method called: analyzeText(String fileName) method that
o Opens the specified file
o Reads the data line by line. After it reads each line, the method should update the value in variable: lineCount
o Splits the line just read into words. After this splitting, the method should count the number of words generated. How can it most easily do that? Once you figure this out, use this count to update the value in variable: wordCount
o Scans the line just read character by character and updates the frequency count for each character. Remember: Only alphabetical characters 'A' .. 'Z' are counted; lower-case letters are mapped to upper-case letters, and all other characters are ignored. Think: what is the best way to scan the line just read? Note that the line just read goes into a string, right? Scanning means looking at each character of the string containing the contents of the line. Which of the various string methods we discussed above is most suitable for looking at each character of the line?
o Now, after figuring out how you will look at each character of the line, you need to figure out how to accomplish the following: suppose the character you just accessed from the line is ‘A’ or ‘a’. This means that in the frequencies array that you have declared in your instance variables, the value in frequencies[0]will be increased by 1. If the next character you accessed is a ‘D’ or ‘d’, then the value in frequencies[3]will be increased by 1. Similarly, on reading ‘Z’ or ‘z’, you will increase the value in frequencies[25]by 1. Think of how to do this? You could use an ‘if’ or ‘switch’ statement of course, but work with your partner, and think (google if necessary) to figure out how you can do this more efficiently.
Use the following TextAnalyzerTester class to test your program:
public class TextAnalyzerTester
{
public static void main(String[] args)
{
TextAnalyzer ta = new TextAnalyzer();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the file: ")
String filename = sc.next();
ta.analyzeText(filename);
System.out.println("Number of Lines: " + ta.getLineCount());
System.out.println("Number of Words: " + ta.getWordCount());
System.out.println("Letter Counts");
int[] frequencies = ta.getFrequencies();
for (int k = 0; k < frequencies.length; k++)
System.out.println("Frequency of " + (char)('A'+ k) +
": "+frequencies[k] + " ");
System.out.println();
}
}
- Relevant Equations
- Java
My current attemp is:
However, when I go to compile, I get an error on my test class which says, "cannot find symbol - method getFrequencies() and cannot find symbol - method getWordCount()". I am confused why I am getting an error when I have a get method for both of those classes.
Java:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TextAnalyzer {
private int lineCount;
private int wordCount;
private int[] frequencies = new int[26];
public int getLineCount() {
return lineCount;
}
public int getWordCount(){
return wordCount;
}
public int getFrequencies(){
return frequencies[frequencies.length];
}
public void analyzeText(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String sc = br.readLine();
lineCount = 0;
while(sc != null) {
lineCount++;
String[] split = sc.split(" ");
wordCount = wordCount + split.length;
findFrequency(split);
sc = br.readLine();
}
}
private void findFrequency(String[] split) {
for(int i = 0 ; i< split.length ; i++ ) {
String str = split[ i].toUpperCase().replaceAll("[^A-Z]","");
for(int j = 0 ; j < str.length() ; j++) {
char c = str.charAt(j);
int position = characterMatch(c);
frequencies(position);
}
}
}
private void frequencies(int poistion) {
for(int i = 0 ; i < frequencies.length ; i++) {
if(poistion == i) {
frequencies[ i] = frequencies[ i] + 1;
}
}
}
private int characterMatch(char c) {
if( c == 'A') {
// A = 0
return 0;
}
else if(c == 'B') {
// B = 1
return 1;
}
else if(c == 'C') {
// C = 2
return 2;
}
else if(c == 'D') {
// D = 3
return 3;
}
else if(c == 'E') {
// E = 4
return 4;
}
else if(c == 'F') {
// F = 5
return 5;
}
else if(c == 'G') {
// G = 6
return 6;
}
else if(c == 'H') {
// H = 7
return 7;
}
else if(c == 'I') {
// I = 8
return 8;
}
else if(c == 'J') {
// J = 9
return 9;
}
else if(c == 'K') {
// K = 10
return 10;
}
else if(c == 'L') {
// L = 11
return 11;
}
else if(c == 'M') {
// M = 12
return 12;
}
else if(c == 'N') {
// N = 13
return 13;
}
else if(c == 'O') {
// 0 = 14
return 14;
}
else if(c == 'P') {
// P = 15
return 15;
}
else if(c == 'Q') {
// Q = 16
return 16;
}
else if(c == 'R') {
// R = 7
return 17;
}
else if(c == 'S') {
// S = 18
return 18;
}
else if(c == 'T') {
// T = 20
return 19;
}
else if(c == 'U') {
// U = 21
return 20;
}
else if(c == 'V') {
// V = 22
return 21;
}
else if(c == 'W') {
// W = 22
return 22;
}
else if(c == 'X') {
// X = 23
return 23;
}
else if(c == 'Y') {
// Y = 24
return 24;
}
else if(c == 'Z') {
// Z = 25
return 25;
}
else {
return -1;
}
}
public static void main(String[] args) throws IOException {
String fileName = "TextAnalyzer.java";
}
}
However, when I go to compile, I get an error on my test class which says, "cannot find symbol - method getFrequencies() and cannot find symbol - method getWordCount()". I am confused why I am getting an error when I have a get method for both of those classes.
Last edited by a moderator: