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

  • SEARCH
  • Home
  • 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 6764569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:36:34+00:00 2026-05-26T14:36:34+00:00

Ok, I’m stumped by this one I have no idea what’s causing this problem.

  • 0

Ok, I’m stumped by this one I have no idea what’s causing this problem. The problem I am having occurs in a class that when given a text file, will find the number of instances of one letter coming after another. So I have a HashMapCharacter, HashMap that stores all of this. The keys of the hash are all the characters contained in the text, and each of these characters corresponds to an inner Hash containing the number of cases of a character(inner hash key) after the outerhash key. The inner hash with key ‘a’ would contain the number of times ‘b’ comes after ‘a’, the number of times ‘c’ comes after ‘a’, the number of times ‘z’ comes after ‘a’, so on and so forth for all the characters that come after ‘a’. MuteableInt just holds an int value, and allows it to be incremented by method.

The error occurs when I compute the total of all the counts contained in each inner Hash. I find the total, and then I insert it under the key ‘~’. The total is correct when I insert it, but when I pull out the total later, I find that for every inner Hash the total takes on the value of the last total I entered.

currHash.put('~', total);

So, when analyzing War and Peace the last total entered is that of ‘x’, which has a total count of 3987. When I pull out the total instance count of ‘h’, or any other character, it is also 3987. Hopefully this makes sense, here is the offending code.

public class CountTransitions {

HashMap<Character, HashMap<Character, MuteableInt>> counts;

public void calcTotal() {
Iterator<Character> iterator = counts.keySet().iterator();
        HashMap<Character, MuteableInt> currHash;
        char curr, next;
        MuteableInt total = new MuteableInt(0);
        MuteableInt count = new MuteableInt(0);

        while (iterator.hasNext()) {
            curr = iterator.next();
            currHash = counts.get(curr);
            total.set(0);
            Iterator<Character> innerIt = currHash.keySet().iterator();
            while (innerIt.hasNext()) {
                next = innerIt.next();
                count = currHash.get(next);
                total.add(count);
            }//end while
            System.out.println("Total: " + total);
            currHash.put('~', total);
        }//end while
}
}//end class
  • 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-05-26T14:36:35+00:00Added an answer on May 26, 2026 at 2:36 pm

    You only ever create one MuteableInt for total, and put this same object reference into each sub-HashMap. This is why they’re changing — it’s one counter you’re incrementing everywhere, not many.

    • 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'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
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
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.