Answer: Fixing Error in TextAnalyzer Java Code

In summary: Unknown character return -1; } } } public static void main(String[] args) { TextAnalyzer textAnalyzer = new TextAnalyzer(); textAnalyzer.analyzeText("file.txt"); }In summary, the textAnalyzer class reads a text file and calculates the line, word, and frequency counts.
  • #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:
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:
Physics news on Phys.org
  • #2
Your main method doesn't call any of the methods of your class. All it does is to declare and initialize a String variable.
Look at the example that is shown in the program problem statement.

Also, your characterMatch method is much longer than it needs to be.
Java:
   private int characterMatch(char c) {
        if( c == 'A') {
            // A = 0
            return 0;
       < snip >
        }
Instead of 26 separate clauses, you could do this:
Java:
   private int characterMatch(char c) {
        return c - 65;
    }
This uses the fact that the ASCII code of 'A' is 65, and the subsequent characters have ASCII values of 66, 67, and so on up to the code for 'Z' of 90.
 
  • #3
Thank you @Mark44

How would I go about calling all of my methods in my main class?

I have also shorten my characterMatch method.
 
  • #4
ParticleGinger6 said:
How would I go about calling all of my methods in my main class?
Just like what is written in the program description.
Use the following TextAnalyzerTester class to test your program:
Java:
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();
    }
}
Look at lines 12, 13, and 15.
 
  • #5
@Mark44

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

    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) {
        return c-65;
    }
}

So I have made a few edits. The only issue I am currently having is that on the Tester code I get the error that getFrequencies is producing a int not an int[]. How am I able to fix that?
 
  • #6
Where is main()? A Java program has to have a method named main.

ParticleGinger6 said:
The only issue I am currently having is that on the Tester code I get the error that getFrequencies is producing a int not an int[]. How am I able to fix that?
You have a method named frequencies, and an array variable named frequencies. At the very least this is confusing. I don't know if Java maintains separate namespaces for methods and variables. If it doesn't, having the same identifier for two separate things is a syntax error.

frequencies should not be a method. The frequencies array should be set exactly as in the sample code given in the problem statement:
Java:
 int[] frequencies = ta.getFrequencies();

Three comments
1. Please use code tags. The Java code just above was written like this:
[code=java] int[] frequencies = ta.getFrequencies(); [/code]
I have added code tags to two of your posts. The code tags preserve your indentation, making the code easier to read.
2. If you have an index variable named i, don't write frequencies[i], because browsers will interpret this to mean "convert everything following to italics." I edited your code to frequencies[ i], with an extra space before i so the conversion to italics wouldn't occur. This is not a shortcoming of Java -- it's just something that happens in browsers.
3. "poistion" is not a word in English. The correct spelling is "position." You can spell words however you want, and the Java interpreter won't care, as long as you are consistent, but some people will notice your spelling errors.
 
  • Informative
Likes berkeman

1. What is the purpose of the "TextAnalyzer" Java code?

The purpose of the "TextAnalyzer" Java code is to analyze a given text and provide information such as the number of words, sentences, and syllables in the text. It also calculates the readability score of the text using the Flesch-Kincaid formula.

2. Why might the "TextAnalyzer" code produce an error?

The "TextAnalyzer" code may produce an error if the text being analyzed contains special characters or symbols that are not accounted for in the code. It may also produce an error if there is an issue with the code itself, such as a syntax error or a logical error.

3. How can I fix an error in the "TextAnalyzer" code?

To fix an error in the "TextAnalyzer" code, you will need to identify the specific error and its cause. If it is a syntax error, you will need to correct the code according to the rules of the Java language. If it is a logical error, you will need to review the code and make necessary changes to ensure it functions correctly.

4. Can I customize the "TextAnalyzer" code for my specific needs?

Yes, you can customize the "TextAnalyzer" code to fit your specific needs. You can modify the code to analyze text in different languages, account for different types of special characters, or add additional features to the code.

5. Are there any resources available for troubleshooting errors in the "TextAnalyzer" code?

Yes, there are many resources available for troubleshooting errors in the "TextAnalyzer" code. You can refer to online forums, Java documentation, or seek help from experienced Java programmers. You can also use debugging tools to identify and fix errors in the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
852
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
984
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
Back
Top