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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:10:10+00:00 2026-05-31T02:10:10+00:00

I have a txt file containing many records, each record has 5 rows and

  • 0

I have a txt file containing many records, each record has 5 rows and are name-value pairs.

I have parsed the file and put the names and values into a hashmap by using a scanner to detect if the file hasNextLine.

At this point because all the records are in the same txt file, the hashmap only contains the last record in the file. This is because the any duplicate keys that comes up will overwrite the value for that key.

My question is how to create a new hashmap for each record?
This is the method i have to build the records:

  public void recordBuilder() throws FileNotFoundException{
    FileReader reader = new FileReader(aFile);
    Scanner outerScan = new Scanner(reader);
    HashMap<String,String> record = new HashMap<String,String>();
    while (outerScan.hasNextLine()){
        Scanner innerScan = new Scanner(outerScan.nextLine());
        try{
            innerScan.useDelimiter(":");
            String name = innerScan.next();
            String value = innerScan.next();
            String rName = name.trim();
            String rValue = value.replaceAll("[^\\w]", "");

            record.put(rName,rValue);
        }
        catch(Exception e){    
        }
    }
    for(String i : record.keySet()){
        System.out.println(i + "\t" + record.get(i));
    }
}
  • 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-31T02:10:11+00:00Added an answer on May 31, 2026 at 2:10 am

    If I understand your question correctly, you have a text file:

     name1:value1
     name2:value2
     name3:value3
     name4:value4
     name5:value5
     name1:value6
     name2:value7
     name3:value8
     name4:value9
     name5:value10
       etc -- name1-5 repeat, with possibly different values
    

    So what you have conceptually is a collection objects, each of which is a set of five name/value pairs, and your objective is to load all of them into memory. You have two choices:

    1. A Map<String,Map<String,String>> – Use this if each group of five name/value pairs contains within it a unique key (maybe the value associated with name1, for instance) that can be used to differentiate the sets.
    2. A List<Map<String,String>> – Use this if the groups do not have a unique key, and the file is just a linear collection of such sets of name/value pairs.

    Here’s an example of the first option; adapting it for the second is left as an exercise:

    String                         firstKey  = "name1";
    Map<String,Map<String,String>> recordset = new HashMap<String,HashMap<String,String>>();
    Map<String,String>             record    = null;
    String                         key       = null;
    
    while (outerScan.hasNextLine()){
        Scanner innerScan = new Scanner(outerScan.nextLine());
        try{
            innerScan.useDelimiter(":");
            String name = innerScan.next();
            String value = innerScan.next();
            String rName = name.trim();
            String rValue = value.replaceAll("[^\\w]", "");
            if (firstKey.equals(name))
            {
                if (record != null) recordset.put(key,record);
                record = new HashMap<String,String>();
                key    = rValue;
            }
            if (record == null) throw new RuntimeException(String.format("First line was not '%s'",firstKey));
            record.put(rName,rValue);
        }
        catch(Exception e){   
            /* DO NOT IGNORE EXCEPTIONS */
        }
        if (record != null) recordset.put(key,record);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a txt file and each line has a set of random strings,
I have a .txt file like: Symbols from __ctype_tab.o: Name Value Class Type Size
I have a txt file containing multiple rows of identical size: (Examples) 0123456 789
I have a 'data.txt' file containing two columns and N rows, corresponding to N
I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have a TXT file containing about 10,000 lines of text. I want to
I have a file words.txt in which each line is a word, followed by
I have a file containing records delimited by the pattern /#matchee/. These records are
Considering i have a 100GB txt file containing millions of lines of text. How
I have a .txt text file, containing some lines.. I load the contain using

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.