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 8035663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:22:23+00:00 2026-06-05T02:22:23+00:00

I’ve been using a hashmap to store unique words from a text file. Now,

  • 0

I’ve been using a hashmap to store unique words from a text file. Now, I need to compare each word in the hashmap with another larger text file and keep track of the frequency of each word as it appears in the text file.

Whilst adding to the hashmap at first, I only insert the key and set the value to 0. My plan is to use ‘value’ as the frequency of each word in the larger text file.

My attempt is as follows; I first use scanner to read the original file and store the words into the hashmap. Next, I use scanner again, but this time associated with the larger text file. From here on, I’m a bit stuck. I don’t know how to update the ‘value’ and index the ‘key’.

Here is what I have;

Scanner fileScanner = new Scanner (new File (fileName));
fileScanner.useDelimiter (" ");

while (fileScanner.hasNext()) {
    for (int i = 0; i < hashmap.size(); i++) {   //This I use to index the key field
        if (hashmap.get(i).equals(fileScanner.next().toString()) {
            int freq ++;
            //How do I update the value field of the corresponding value?
        }
    }
}

Now, obviously, nothing in the above code works, and I’m having some problems with figuring out a way. Could anyone please help me?

  • 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-05T02:22:24+00:00Added an answer on June 5, 2026 at 2:22 am

    If you try to count number of words and store it as map then when new words is added try puting value 1 not 0 (word exist at least once).

    For update check if map contains value for key, then put it once again with incremented value. Old value will be replaced.

    Try this

    HashMap<String, Integer> hashmap = new HashMap<String, Integer>();
    String key = "myWord";
    hashmap.put(key, 1);
    Integer tmp = null;
    // lets increment value if exist in map or put new value if doesn't exits in map
    if ((tmp = hashmap.get(key)) != null) {
        //if map contains word
        hashmap.put(key, tmp + 1);
    } else {
        //if word is new, map does't contain it as key 
        hashmap.put(key, 1);
    }
    System.out.println(hashmap);
    //out ->{myWord=2}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am using parse_ini_file to read the contents of a file however it is

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.