Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to use recursion to parse groups inside groups in Java?

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use recursion to parse groups inside groups in Java?

    I'm just starting with recursion, so if someone can try to help I will very much appreciate it.

    I have the following `String`:
        field1: typeA
        field2: typeA
        field3: typeB
        group1:
            fieldA: typeA
            fieldB: typeB
            subGroup1:
                fieldX: typeB
                fieldY: typeB
            fieldC: typeC
        field4: typeA
        group2:
            fieldA: typeB
            subGroup1:
                fieldX: typeA
                fieldY: typeA
            fieldB: typeA
            fieldC: typeC
        field5: typeC
        field6: typeD
    Note that I can have groups inside groups (subgroups). I can also have groups inside groups inside groups, so recursion is mandatory as far as I know.

    Note that the *spacing* in the beginning of each line determines if the field belongs to the group or not.

    I would like to write a method in Java that will return a `LinkedHashMap<String, Object>`, where the *Object* map value can either be a `String` denoting that this is a field (not a group) or another `LinkedHashMap<String, Object>` denoting that this is a group (not a field).

    I've tried but failed miserably. Recursion is just too hard for me at the moment, but I'm studying it.

    To help whoever wants to help, below the code:
        	public static void main(String[] args) {
     
        		String text = String.join("\n",
     
        				"field1: typeA",
        				"field2: typeA",
        				"field3: typeB",
        				"group1:",
        				"    fieldA: typeA",
        				"    fieldB: typeB",
        				"    subGroup1:",
        				"        fieldX: typeB",
        				"        fieldY: typeB",
        				"    fieldC: typeC",
        				"field4: typeA",
        				"group2:",
        				"    fieldA: typeB",
        				"    subGroup1:",
        				"        fieldX: typeA",
        				"        fieldY: typeA",
        				"    fieldB: typeA",
        				"    fieldC: typeC",
        				"field5: typeC",
        				"field6: typeD",
     
        				"");
     
        		Map<String, Object> solution = doIt(text);
        		System.out.println(solution);
        	}
     
        	private static LinkedHashMap<String, Object> doIt(String text) {
        		// solution goes here...
        		return null;
        	}
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to use recursion to parse groups inside groups in Java?

    I've tried but failed miserably.
    Can you post the code you tried?

    Be sure to add some comments to the code describing what it is supposed to do.
    Also an example of the output would be useful.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use recursion to parse groups inside groups in Java?

    Can you post the code you tried?
    I have nothing good to show. I got stuck, my brain went into a deadlock!


    Here is the correct output:
        public static void main(String[] args) {
        	Map<String, Object> map = new LinkedHashMap<String, Object>();
     
        	map.put("field1", "typeA");
        	map.put("field2", "typeA");
        	map.put("field3", "typeB");
     
        	Map<String, Object> map2 = new LinkedHashMap<String, Object>();
        	map2.put("fieldA", "typeA");
        	map2.put("fieldB", "typeB");
     
        	Map<String, Object> map3 = new LinkedHashMap<String, Object>();
        	map3.put("fieldX", "typeB");
        	map3.put("fieldY", "typeB");
        	map2.put("subGroup1", map3);
     
        	map2.put("fieldC", "typeC");
     
        	map.put("group1", map2);
     
        	map.put("field4", "typeA");
     
        	Map<String, Object> map4 = new LinkedHashMap<String, Object>();
        	map4.put("fieldA", "typeB");
     
        	Map<String, Object> map5 = new LinkedHashMap<String, Object>();
        	map5.put("fieldX", "typeA");
        	map5.put("fieldY", "typeA");
        	map4.put("subGroup1", map5);
     
        	map4.put("fieldB", "typeA");
        	map4.put("fieldC", "typeC");
     
        	map.put("group2", map4);
     
        	map.put("field5", "typeC");
        	map.put("field6", "typeD");
     
        	System.out.println(map);
        }
    The map matches the original text as you can see below:
    {field1=typeA, field2=typeA, field3=typeB, group1={fieldA=typeA, fieldB=typeB, subGroup1={fieldX=typeB, fieldY=typeB}, fieldC=typeC}, field4=typeA, group2={fieldA=typeB, subGroup1={fieldX=typeA, fieldY=typeA}, fieldB=typeA, fieldC=typeC}, field5=typeC, field6=typeD}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to use recursion to parse groups inside groups in Java?

    I'm not good at recursive either. This site has lots of programmers that like to show off their programming skills. Try them:
    http://www.coderanch.com/forums
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 17
    Last Post: January 16th, 2021, 02:05 PM
  2. Parse XML file with java
    By ltgroup in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 1st, 2018, 08:16 PM
  3. Replies: 4
    Last Post: April 13th, 2013, 02:40 AM
  4. Replies: 0
    Last Post: February 23rd, 2010, 02:12 PM