Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:35:45+00:00 2026-06-13T13:35:45+00:00

I’m making a class which parses through all the chars in a file and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:35:46+00:00Added an answer on June 13, 2026 at 1:35 pm

    This is comparing object reference only to be same and also instance being the Character not CharProfile.

    @Override
    public boolean equals(Object o) {
        if (this.character == ((Character)o)) {
            return true;
        }
        else {
            return false;
        }
    }
    

    You need to change your equals method to accept the instance of CharProfile and compare the character value inside it as below:

    @Override
    public boolean equals(Object o) {
        if(this.character == ((CharProfile)o).getCharacter()) {
            return true;
        }else {
            return false;
        }
    }
    

    EDIT: your equal method is not being called because you are passing char to contains method.

    Please change that to :

             // Constructs the list of chars and their respective number of occurrences
             CharProfile charProfile = new CharProfile(wordArray[i]);
             if (!charOccurrences.contains(charProfile)) {
                  charOccurrences.add(charProfile);
             }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.