Jump to content
  • 0

Help With A Parsing Code!


Alianza

Question

Hello guys,
I'm here with a new code I'm trying to make that I cannot get it out of my head  :rage:
So, here's my problem: I have two tables with information. Both of them have the same structure (ID, Value) but one of them has more repeated values than the other one. For example:
|----------------------|                    |----------------------|

| ID              Val   |                    | ID             Val    |

|----------------------|                    |----------------------|

| 1                 0    |                    | 1                 0    |

| 1                 0    |                    | 1                 0    |

| 2                 5    |                    | 1                 0    |

| 2                 5    |                    | 1                 0    |

| 2                 5    |                    | 2                 5    |

| 2                 5    |                    | 2                 5    |

| 3                 1    |                    | 2                 5    |

| 3                 1    |                    | 2                 5    |

-----------------------                     | 3                 1    |

                                                | 3                 1    |

                                                ------------------------

 

Here you can see two tables with the same structure but one of them has more repeated values than the other one (as I said before). What I'm trying to make, is the following: take the ID from the first table (ID = 1) and copy the value from the second table (Val = 0), but just copy it as many times as the ID in the first table exists. For example: if the ID 1 in the first table exists 2 times, copy the Val from the second table 2 times, skip the other two, and go to the next ID by doing the same.

I've tried to do it. The class I've made loads both tables and fills two ArrayLists with its information by putting them into an IntIntHolder class (thks aCis) in order to compare them later. So far, this is the code I've written, but it throws NoSuchElementException. And running it without the (see below), it prints some data but not all (if total data is 8, it prints 4).

while (elementA.getId() != elementB.getId())

Here's my code:

public void compareAndPrint()
{
	Iterator<IntIntHolder> iterA = listA.iterator();
	Iterator<IntIntHolder> iterB = listB.iterator();
	while (listA.hasNext())
	{
		IntIntHolder elementA = iterA.next();
		IntIntHolder elementB = iterB.next();
		if (elementA.getId() == elementB.getId())
			System.out.println(elementB.getValue());
		else
		{
                        while (elementA.getId() != elementB.getId())
			        iterB.next();
		}
	}
}

So ye... that's all. I'm trying to parse some data from the system so that's why I'm trying to do this.

Thanks for reading, and I'm looking forward to your answers!

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Okay mate. I will try to do so and I'll keep you updated.

Thank you!

 

EDIT: hehe, I've solved it following your steps.

Here's the code:

private final static Map<Integer, Integer> MAP = new LinkedHashMap<>();

public void compareAndGenerate()
{
	int i = 1;
	for (IntIntHolder aElement : listA)
	{
		if (!MAP.containsKey(aElement.getId()))
			i = 1;
		
		MAP.put(aElement.getId(), i++);
	}
	
	for (Entry<Integer, Integer> map : MAP.entrySet())
	{
		int ii = map.getValue();
		for (IntIntHolder bElement : listB)
		{
			if (bElement.getId() == map.getKey())
			{
				while (ii > 0)
				{
					System.out.println(bElement.getValue());
					ii--;
				}
			}
		}
	}
}

Thank you, very much.

You disserve a cookie. Here ya' go

 

Cookie-Download-PNG.png

Edited by Alianza
Link to comment
Share on other sites

  • 0

What happens if a same id got different values ?

 

What is supposed to be the third table, since I didn't understand anything (I don't see the point between the 2 tables at all, until you want to increment it but it's probably far better to simply use a Map<Integer, Integer> and increment it directly the second parameter) :D.

Link to comment
Share on other sites

  • 0

Hello Tryskell, and thanks for replying!

Well, I'm trying to parse some system data as I said before, so I need to maintain those values as they appear there. I need to copy something from a higher chronicle to Interlude, which has less data.

For example:

post-208767-0-08541200-1499956043_thumb.png

 

Those are both tables. I'm trying to copy the values from the B table to the A table. Sometimes, the B table has more repeated values which increment its size compared to A table. What I'm trying to do, its to past through those repeated values and keep copying the other values that are not repeated and match with the A table (original Interlude data).

Is it clearer now?

 

EDIT: I forgot to say that I need to maintain the size. I cannot use a Map<Integer, Integer> because that will affect its size. I just need to copy values from table B to the IDs of  table A.

Edited by Alianza
Link to comment
Share on other sites

  • 0

No clue if there is an easier solution, I would count occurences of each id type on table A (with a stream() you can use .count() and can generate a temporary map holding id/occurences count), then on table B you for loop the whole thing, verify the current id and current occurence number. If occurence counter is superior to occurence count of the temp map, you "continue;". Once you changed of id you reset the occurence counter to 0.

Link to comment
Share on other sites

  • 0

I cant see the logic there, sorry (I'm half stupid).

My logic is: when the ID of the A table and the ID of the B table are different, I just use iterB.next() to find the ID that matches the ID of the table A again and continue with the code. Am I wrong?

For some reason I cant do it (maybe because I'm half stupid).

Thanks again, Tryskell.

Link to comment
Share on other sites

  • 0

If id/values are correct on table 2, you only have to filter the number of IntIntHolder from this table to fit the count of table 1.

 

So you count occurences of table 1 : id 1 got 2 matches, id 2 got 4 matches, id 3 got 2 matches. You keep result in a temporary map.

 

Then you for loop the table 2 ; based on temporary map, you find id 1. Based on your temp map, it should have 2 occurences, but you find more. You add the 2 first, and don't bother about leftover (continue). Then you got on another id, etc etc.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Posts

    • L2 Insignia High Five 20x MID SERVER FOCUSED ON OLYMPIAD | PVP | AUTOFARM       OPEN BETA TEST SERVER 3 MAY 2024   GRAND OPENING 10 MAY 2024    Rates: 📜 XP/SP 20x | Spoil 15x | Drop 10x | Adena 10x 📜     Server Features: 🔥 No Olf-T Shirt, No big over-enchant, No over-power Donate 🔥 🔥 VIP Gold Color Chat, Unique Olympiad Extra Points Engine, GvG Event 🔥 🔥 Auto-Farm, LoA and DV scheduled PvP Zone, Calendar Daily Reward 🔥 🔥 Castle Instance, Solo Instance, PvP Solo Rift, Dress me system, Adena Boxes 🔥        Website: https://www.l2insignia.com  Discord: https://discord.com/invite/yEgsrHn2hQ      
    • I am selling the essence project which includes versions 388 and 439 that have been running for over 2 years or (447 as custom PVP like Pride). I have a test server for you to test them out. If you are really interested in it then contact my seller at discord: kiwi7106. Price: 4000 Euro P/s: This is a project that I have spent a lot of money and time developing, so if you are not interested in it, please get out of this topic, thank you. P/s 2: If you find the price too expensive, it's best to skip this article and find another project and don't comment negatively on my topic, thank you.
    • Someone ask me for this, it should work on any client that has Kamael race, preview:     Installation - there are two ways to install depending on how you want to use it:   Method 1: If you want to completely replace the original, do:   Copy all lines from your armorgrp to Notepad++, press Ctrl+H, check the "match whole word" option and replace:   kamael.Mkamael_m000_w_ad00   by:   AvengersKamaelWings.Avengers_MKamael_m001_w_ad00   Then replace:   MKamael.Mkamael_m000_t00_w   by:   AvengersKamaelWings.MKamael_m001_t00_w   Now repeat the same process with the female, replace:   kamael.Fkamael_m000_w_ad00   by:   AvengersKamaelWings.Avengers_FKamael_m001_w_ad00   Then replace:   FKamael.Fkamael_m000_t00_w   by:   AvengersKamaelWings.FKamael_m001_t00_w   You're done, paste everything back into File Edit and save!   Method 2: If you only want to replace in specific sets, execute the above process only on the armorgrp of those sets.   Repack by: AvengersTeamBr Password: LadrãoDeFrango      
  • Topics

×
×
  • Create New...