-3

Design a program that reads an ASCII text file (provided) one byte at a time and produces an output file that contains the frequency count of how many times each character appears in the input file. Do not sort the output. A character frequency class instance must be used to represent each unique character in the file (see below). For this exercise, the character frequency objects must be processed and stored using an array.

I have tried creating an object of Character Frequencies and using an equals method to compare it with input.

The array is supposed to be storing new characters and frequency of characters but it is also storing duplicates and frequency is not accurate.

public class CharacterFrequency {
//Stores character by character from input file to check if its in ASCII 
array
private static char character;
//Stores number of times character shows up in input file
private static int frequency = 0;
private static CharacterFrequency arr[] = new CharacterFrequency[256];
//Constructor class for CharacterFrequency
public CharacterFrequency(char character, int frequency){
    setCharacter(character);
    setFrequency(frequency);
}

public static void main(String[] args){
    //initiate object array to store character and frequency variables

        File file = new File("C:/Users/alber/Desktop/sample.txt");
        if (!file.exists()) {
          System.out.println(args[0] + " does not exist.");
          return;
        }
        if (!(file.isFile() && file.canRead())) {
          System.out.println(file.getName() + " cannot be read from.");
          return;
        }
        try {       

            FileReader fr = new FileReader("C://Users/alber/Desktop/sample.txt");               

                    //loop through character array 

                    for(int i = 0; i < arr.length; i++){
                    //read file character by character until no characters 
  are left and store character each time 

                    int j = 0;
                    while ((j=fr.read()) != -1){

                        character = (char)j;
                        //if character is in the array already do not add 
 it just call increment method                                      
                        //if(arr[i].equals(character)){ 
                        if(arr[i] == null || (! 
(arr[i].equals(character)))){
                            arr[i] = new 
  CharacterFrequency(character,frequency);
                            System.out.println(arr[i]);
                        //if character is not in the array add it to it  
                        }else{                                  
                            increment();    


                        }//end else   

                    }//end while

                    }//end for

             fr.close();                 
        } catch (IOException e) {
          e.printStackTrace();
        }

    }
public char getCharacter(){
    return character;
}

public static void setCharacter(char character){

}

public int getFrequency(){
    return frequency;
}

public void setFrequency(int frequency){
    this.frequency = frequency;
}

public static void increment(){
    frequency++;
}
@Override
public boolean equals(Object obj){

    if(obj == null || obj.getClass()!= this.getClass()){
    return false;
    }
    if(obj == this){
    return true;
    }

    CharacterFrequency c = (CharacterFrequency)obj;
    return CharacterFrequency.character == this.character && CharacterFrequency.frequency == this.frequency;
}

public String toString(){

    return character + " " + frequency;
}

}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Any help is greatly appreciated. Been stuck on this for awhile. – Albert Calamatta Sep 21 '19 at 06:45
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joakim Danielson Sep 21 '19 at 07:12
  • I fixed the null pointer exception. But I am still having problems with my equals method and my program is saving duplicate character values to the array of objects – Albert Calamatta Sep 21 '19 at 07:24
  • you have initalized the array arr but no data is present in the array. you need to populate the array inorder to use it for comparison using equal i mean the data present in arr[0] is still null. – Sujay Mohan Sep 21 '19 at 08:02
  • Sujay, thanks for the reply. The problem is I would be populating the array with 256 ascii characters and frequencies. Thats why I am looking to add new characters to the array as they come. I have been able to print all characters from input file to object array but it is adding duplicates – Albert Calamatta Sep 21 '19 at 08:13
  • Still in your logic there is a problem. in the loop you are calling increment method. this will increment the frequency but it will not increment the frequency of the object inside arr[i]. If you want increment the frequency of that object you need to call arr[i].increment(). – Sujay Mohan Sep 21 '19 at 17:30

1 Answers1

0

You have created the array in the below line.

CharacterFrequency arr[] = new CharacterFrequency[256];

There are no elements added in the array. So when you access arr[j] the object you are accessing is null.when you call equals in a null object you will get a NullPointerException.

Sujay Mohan
  • 933
  • 7
  • 14
  • Sujay, thanks for the reply. The problem is I would be populating the array with 256 ascii characters and frequencies. Thats why I am looking to add new characters to the array as they come. I have been able to print all characters from input file to object array but it is adding duplicates – Albert Calamatta Sep 21 '19 at 08:18
  • you can use hashmap for this purpose. which will not hold duplicates and value of the hashmap can be frequency. Whenever a character is read, if it is already present in hashmap, read the value increment it by one and update the hashmap with new value. If the character is not present in hashmap you add the key as the character and value should be 1. That should suffice your requirement. you can print the entire hashmap which will give the character set and count of each character used in the file. – Sujay Mohan Sep 21 '19 at 08:36
  • Sujay, Great thoughts and that would work. The only thing is that I am required to use an array as a data structure. – Albert Calamatta Sep 21 '19 at 10:02