0

I am working with an input file that has a list of guild names and then guild servers for those guild names. My problem is that for some guilds there is a duplicate, meaning that the same guild exists on different servers. So I need the guildName, read from my input file, to display the different server it is on. Here is the code I have so far:

private void buildGuilds()
    {
        using (StreamReader inFile = new StreamReader("guilds.txt"))
        {
            string str = null;
            char[] delimiters = {'-', '\t'};

            //while the buffer isn't empty
            while ((str = inFile.ReadLine()) != null)
            {
                SortedList<string, string> guild = new SortedList<string, string>();
                string[] Buffer = str.Split(delimiters);
                guildName = Buffer[1];
                guildServer = Buffer[2];


                    if (!dictGuilds.ContainsKey(guildName))
                    {
                        dictGuilds.Add(guildName, new List<string>());
                    }
                    dictGuilds[guildName].Add(guildServer);

So my program reads the data into 2 variables and then determines what values go where but I cannot get it to print when using the conventional foreach pair.Key pair.Value method. Here is my print method as well.

private void printGuilds()
    {
       foreach (KeyValuePair<string, List<string>> pair in dictGuilds)
        {
            Guilds_List.Items.Add(pair.Key + pair.Value);
        }
    }

any help i could get would be great. Thank you so much

  • It looks like you already build name to list of servers map correctly... So for printing list https://stackoverflow.com/questions/52927/console-writeline-and-generic-list (currently as duplicate) should work. If you have some other problem - [edit] post and clarify. Also make sure code in the post is [MCVE] and not some collection of lines from middle of some code. – Alexei Levenkov Feb 09 '19 at 23:06

1 Answers1

0

I think the below may be what you're looking for, though I'm not certain exactly what you're seeking to do (feel free to let me know if I'm missing something).

I achieved the following by:

  • Creating an empty class library project
  • Adding the xunit and fluentassertions nuget packages.

This allows you to run the unit tests that verify this assertion.

If I misunderstood the question, let me know and I'll try to adapt it for you. Note that I changed the input from a file input for the purposes of writing the tests & verifying things.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentAssertions;
using Xunit;

namespace MultiValueDictionaryTests
{
    public class MultiValueDictionaryTests
    {
        [Fact]
        public void UnderstandsPairsCorrectly()
        {
            /* Mimics the following file contents:
             Guild1-ServerDEF
             Guild2-ServerDEF
             Guild2-ServerABC
             Guild1 ServerXYZ
             Guild2-ServerABC
             Guild2-ServerABC
             */
            var testString = "Guild1-ServerDEF\r\nGuild2-ServerDEF\r\nGuild2-ServerABC\r\nGuild1\tServerXYZ\r\nGuild2-ServerABC\r\nGuild2-ServerABC\r\n";

            var builder = new GuildBuilder();

            var result = builder.BuildGuilds(testString);

            result.Should().ContainKey("Guild1");
            result.Should().ContainKey("Guild2");

            result["Guild1"].Should().ContainKey("ServerDEF")
                .And.ContainKey("ServerXYZ");
            result["Guild1"].Should().NotContainKey("ServerABC");

            result["Guild2"].Should().ContainKey("ServerDEF")
                .And.ContainKey("ServerABC");

            result["Guild2"].First().Key.Should().Be("ServerABC");
        }

        [Fact]
        public void WriteLine_ShowsCommaSeparatedValues()
        {
            var builder = new GuildBuilder();

            var example = new SortedDictionary<string, SortedList<string, string>>
            {
                {
                    "Guild1", new SortedList<string, string> {{"ServerABC", "ServerABC"}, {"ServerDEF", "ServerDEF"}}
                },
                {
                    "Guild2", new SortedList<string, string> {{"ServerABC", "ServerABC"}, {"ServerXYZ", "ServerXYZ"}}
                }
            };

            List<string> resultLines = builder.WriteGuildLines(example);

            resultLines.First().Should().Be("Guild1 - ServerABC, ServerDEF");
            resultLines.Last().Should().Be("Guild2 - ServerABC, ServerXYZ");
        }
    }

    public class GuildBuilder
    {
        readonly char[] delimiters = { '-', '\t' };

        public SortedDictionary<string, SortedList<string,string>> BuildGuilds(string inputString) // instead of file
        {
            var result = new SortedDictionary<string, SortedList<string,string>>();
            using (var reader = new StringReader(inputString))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    var splitArray = line.Split(delimiters);
                    var guild = splitArray[0];
                    var server = splitArray[1];

                    if (!result.ContainsKey(guild))
                    {
                        result.Add(guild, new SortedList<string,string>());
                    }

                    if (!result[guild].ContainsKey(server))
                    {
                        result[guild].Add(server, server);
                    }

                }
            }

            return result;
        }

        public List<string> WriteGuildLines(SortedDictionary<string, SortedList<string, string>> input)
        {
            var result = new List<string>();

            foreach (var item in input)
            {
                result.Add($"{item.Key} - {string.Join(", ", item.Value.Keys)}");
            }

            return result;
        }
    }
}
SeanKilleen
  • 8,809
  • 17
  • 80
  • 133
  • OP already has code to build list of values for each key... they are asking about showing list with `WriteLine` I believe. – Alexei Levenkov Feb 09 '19 at 23:02
  • @AlexeiLevenkov's comment makes sense to me; I've updated my example to try to include a method for doing this, but agree that the OP may want to review those links in the comments or clarify the question. – SeanKilleen Feb 09 '19 at 23:16