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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:56:51+00:00 2026-05-16T14:56:51+00:00

I am attempting to the read-in a .txt file and create a map to

  • 0

I am attempting to the read-in a .txt file and create a map to calculate total property listed in dollars and cents for each agent id. The agent ids are the three digit numbers at the end of each line. The problem is, I’m not exactly sure how to go about doing this.

I’m thinking I should create a while loop and add each line to an arraylist, and then….. I’m lost.


110001 commercial 500000.00 101
110223 residential 100000.00 101
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110

Ok, I know It has been a long time since I started this thread, but I haven’t been able to revisit this problem for weeks now. (I’m attempting to learn this for personal growth, so it hasn’t been priority) So far I have come this far…

My method for creating the map is this:

public void createMap()
{
    if(in != null)
    {
        while(in.hasNextLine())
        {
            String s = in.nextLine().trim();
            System.out.print(s);


            String[] p = s.split(" ");

            String agentId = p[3];

            double property = Double.valueOf(p[2]);

            if(!map.containsKey(agentId)){
                //if not then add the new key with new value
                map.put(agentId, property);
                in.nextLine();
            }
            else
            {  //if key is present, add the value..
                map.put(agentId, map.get(agentId) + property);
                in.nextLine();
            }


        }
    }
}

Then in my program that I am running:
I convert map to a Set and using a enhanced for-loop print each key and value.

Set<String> keySet = examp.map.keySet();
    for (String key : keySet)
    {
        double value = examp.map.get(key);
        System.out.println(key + "->" + value);
    }

my output however gives me this: {} “empty brackets”!
What am I doing wrong?

  • 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-16T14:56:51+00:00Added an answer on May 16, 2026 at 2:56 pm

    If you have it in an arraylist, you could do something like:

    HashMap<String, Integer> result = new HashMap<String, Integer>(); //<agent id, amount>
    for(String line: arraylist)
    {
        String[] items = line.split(" ");
        String agent = items[3];
        double amount = Double.valueOf(items[2]);
    
        if(!result.containsKey(agent)
            result.put(agent, amount);
        else
            result.put(agent, result.get(agent) + amount);
    }
    

    You can iterate over the hash map’s keys to get the results.

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

Sidebar

Related Questions

I am attempting to read in from a txt file strings that are very
I'm attempting to read the file.txt into java line by line and then when
I'm attempting to read a property on a series of Sprites. This property may
I'm currently attempting to read information about an object from the web.config file and
import string file = open('Text.txt') dataArray = file.read() file.close() dataArray = str(dataArray) letters =
I am attempting to read a file and set the text of a richTextBox
I'm attempting to read from a file a customer's name, id, and loan information.
I'm attempting to read an input file line by line which contains fields delimited
I am attempting to read a bitmap manually. So I read the bitmap file
Well, I am attempting to read a text file that looks like this: FTFFFTTFFTFT

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.