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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:44:11+00:00 2026-06-13T10:44:11+00:00

Basically, I got a file containing details about people, each person separated by a

  • 0

Basically, I got a file containing details about people, each person separated by a new line, e.g
“

name Marioka address 97 Garderners Road birthday 12-11-1982 \n
name Ada Lovelace gender woman\n
name James address 65 Watcher Avenue

” and so on..

And, I would like to parse them to [Keyword : Value] pair array, such as

{[Name, Marioka], [Address, 97 Gardeners Road], [Birthday, 12-11-1982]},
{[Name, Ada Lovelace], [Gender, Woman]}, and so on....

and so on. The keywords will be a set of defined words, in above case: name, address, birthday, gender, etc…

What is the best way to do this?

This is how I did it, it works but was wondering whether there are better solutions.

    private Map<String, String> readRecord(String record) {
        Map<String, String> attributeValuePairs = new HashMap<String, String>();
        Scanner scanner = new Scanner(record);
        String attribute = "", value = ""; 

        /* 
         * 1. Scan each word. 
         * 2. Find an attribute keyword and store it at "attribute".
         * 3. Following words will be stored as "value" until the next keyword is found.
         * 4. Return value-attribute pairs as HashMap
         */

        while(scanner.hasNext()) {
            String word = scanner.next();
            if (this.isAttribute(word)) {
                if (value.trim() != "") {
                    attributeValuePairs.put(attribute.trim(), value.trim());
                    value = "";
                }
                attribute = word;
            } else {
                value += word + " ";
            }
        }
        if (value.trim() != "") attributeValuePairs.put(attribute, value);

        scanner.close();
        return attributeValuePairs;
    }

    private boolean isAttribute(String word) {
        String[] attributes = {"name", "patientId", 
            "birthday", "phone", "email", "medicalHistory", "address"};
        for (String attribute: attributes) {
            if (word.equalsIgnoreCase(attribute)) return true;
        }
        return false;
    }
  • 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-13T10:44:12+00:00Added an answer on June 13, 2026 at 10:44 am

    To extract values from a string, use regular expressions. I expect you to know how to read each line from a file and how to build up an array with the results.

    Still this is not a good solution, since it doesn’t work if any of the keywords are included in the name or address… But that’s what you asked for…

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
    
        public static void main(String[] args) {
    
            Pattern p = Pattern.compile("name (.+) address (.+) birthday (.+)");
    
            String text = "name Marioka address 97 Garderners Road birthday 12-11-1982";
    
            Matcher m = p.matcher(text);
    
            if (m.matches()) {
                System.out.println(m.group(1) + "\n" + m.group(2) + "\n"
                        + m.group(3));
            } else {
                System.out.println("String does not match");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a text file, with about 200,000 lines. Each line represents an object
So basically I've got something like this at the top of my tex file:
I've got a file of blocks of strings, each which end with a certain
I've basically got an XML file full of product information for use in an
I basically downloaded a file name custom.mp3 into my isolatedstorage and I can see
We've got a MsBuild.proj file which contains the following section (simplified): <Target Name=WEB DependsOnTargets=CleanResults;UpdateAssemblyInfo;Services;Business>
Basically I got a PHP file that returns an array of different results (the
I've got a weird issue while trying to decrypt an encrypted text file. Basically
I've basically got a table with 2 rows and it looks like this: ---------
Basically i got this for loop and i want the number inputed (eg. 123)

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.