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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:34:36+00:00 2026-06-18T11:34:36+00:00

I have file which has String in the form key/value pair like people and

  • 0

I have file which has String in the form key/value pair like people and count, example would be

"Reggy, 15"
"Jenny, 20"
"Reggy, 4"
"Jenny, 5"

and in the output I should have summed up all count values based on key so for our example output would be

“Reggy, 19”
“Jenny, 25”

Here is my approach:

  1. Read each line and for each line get key and count using scanner and having , as delimiter
  2. Now see if key is already present before if then just add currentValues to previousValues if not then take currentValue as value of HashMap.

Sample Implementation:

public static void main(final String[] argv) {
    final File file = new File("C:\\Users\\rachel\\Desktop\\keyCount.txt");

    try {
        final Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            if (scanner.hasNext(".*,")) {
                String key;
                final String value;

                key = scanner.next(".*,").trim();

                if (!(scanner.hasNext())) {
                    // pick a better exception to throw
                    throw new Error("Missing value for key: " + key);
                }

                key = key.substring(0, key.length() - 1);
                value = scanner.next();

                System.out.println("key = " + key + " value = " + value);
            }
        }
    } catch (final FileNotFoundException ex) {
        ex.printStackTrace();
    }
}

Part I am not clear about is how to divide key/value pair while reading them in and creating HashMap based on that.

Also is the approach am suggestion an optimal one or is there a way to enhance the performance more.

  • 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-18T11:34:37+00:00Added an answer on June 18, 2026 at 11:34 am

    Since this is almost certainly a learning exercise, I’ll stay away from writing code, letting you have all the fun.

    Create a HashMap<String,Integer>. Every time that you see a key/value pair, check if the hash map has a value for the key (use ‘containsKey(key)’). If it does, get that old value using get(key), add the new value, and store the result back using put(key, newValue). If the key is not there yet, add a new one – again, using put. Don’t forget to make an int out if the String value (use Integer.valueOf(value) for that).

    As far as optimizing goes, any optimization at this point would be premature: it does not even work! However, it’s hard to get much faster than a single loop that you have, which is also rather straightforward.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file called messages.properties which has lines with syntax <key>=<string> . Each
I have a JSON string (external file) which has an element which can either
I have a text file which has a particular line something like sometext sometext
I have a php file which has some variables like $name1, $name2... How can
I have written a form which has 3 text inputs and one file input.
I have a file which has multiple columns, whitespace separated. e.g: data1 data2 data3
Little Background: I have csv file which has lots of rows and each row
Assume you have a file which has been committed in your Git repo. You
I'm using Python, and I have a file which has city names and information
I have a javascript file which has a function calling a server (url) to

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.