I’m making a class which parses through all the chars in a file and finds the amount of occurrences of each letter.
At one step, as I’m going through the file adding the characters, I check if the list of characters already contains the particular character I’m at. If it doesn’t, it adds it to the list with an occurrence value of 1, and if it does already contain it, it increments the value of occurrence by 1.
However, my below code is not properly figuring out whether or not the list already contains a specific character.
I’m using an ArrayList, and I know I have to override the equals method in my CharProfile class, which I did, in order to have the equals() method that contains() uses work properly.
overrode equals method in CharProfile:
@Override
public boolean equals(Object o) {
if (this.character == ((Character)o)) {
return true;
}
else {
return false;
}
}
Code:
import java.util.*;
import java.io.*;
public class HuffmanCoder {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the name of the file to be read from: ");
String fileName = keyboard.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
int charCount = 0;
ArrayList<CharProfile> charOccurrences = new ArrayList<CharProfile>();
while (inputFile.hasNext()) {
// Grabs the next word in the file and converts it into a char array
String word = inputFile.next();
char[] wordArray = word.toCharArray();
// Parses the word on a char-by-char basis
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i] != ' ') {
charCount++;
// Constructs the list of chars and their respective number of occurrences
if (!charOccurrences.contains(wordArray[i])) {
charOccurrences.add(new CharProfile(wordArray[i]));
}
else {
for (int j = 0; j < charOccurrences.size(); j++) {
if (charOccurrences.get(j).getCharacter() == wordArray[i]) {
charOccurrences.get(j).incremementOccurrences();
break;
}
}
}
}
}
}
// Figure out each char's probability
for (int i = 0; i < charOccurrences.size(); i++) {
System.out.println(charOccurrences.get(i).getCharacter());
}
}
}
Full CharProfile class:
public class CharProfile {
private char character;
private int occurrences;
private double probability;
public CharProfile(char character) {
this.character = character;
occurrences = 1;
}
public void incremementOccurrences() {
occurrences++;
}
public char getCharacter() {
return character;
}
public void setCharacter(char character) {
this.character = character;
}
public int getOccurrences() {
return occurrences;
}
public void setOccurrences(int occurrences) {
this.occurrences = occurrences;
}
public double getProbability() {
return probability;
}
public void setProbability(double probability) {
this.probability = probability;
}
public boolean equals(char character) {
if (this.character == character) {
return true;
}
else {
return false;
}
}
@Override
public boolean equals(Object o) {
if (this.character == ((Character)o)) {
return true;
}
else if (this.character == ((Character)o).charValue()) {
return true;
}
else {
return false;
}
}
}
Issue in a nutshell: Instead of return true when checking if the list already contains that char, it seems to always return false, resulting in every single char in the file being added to the list, when it should only be unique ones.
This is comparing object reference only to be same and also instance being the
CharacternotCharProfile.You need to change your equals method to accept the instance of
CharProfileand compare the character value inside it as below:EDIT: your equal method is not being called because you are passing char to
containsmethod.Please change that to :