WhynarySearch
WhynarySearch

Reputation: 217

Trying to turn a .txt file into a dictionary of words. Where am I going wrong? (C#)

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace test1
{
    class Program
    {

        static void Main(string[] args)
        {
            var lines = File.ReadAllLines(@"C:/temp/TextFile.txt").Select(x => x.Split('/')).FirstOrDefault();
            var words = new Dictionary<string, string>();

            for (int i = 0; i < lines.Length - 1; i += 2)
            {
                words.Add(lines[i], lines[i + 1]);
            }

            Console.WriteLine(words);
            Console.ReadKey();
        }
    }
}

When I run it this appears:"System.Collections.Generic.Dictionary`2[System.String,System.String]"

Can somebody please tell me how to fix this code or if you know of an better way of doing what I am trying to do.

-Thanks

Upvotes: 3

Views: 63

Answers (2)

TuneNCode
TuneNCode

Reputation: 11

You are doing it all well except for trying to print a whole dictionary in one line of code.

Instead you could print the word as you add it to the dictionary, or you could do a foreach loop on the dictionary object to print each Key-Value-Pair, or you could write a Helper/Extension method to handle WriteLine on a Dictionary.

Here scenario 1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace test1
{
    class Program
    {

        static void Main(string[] args)
        {
            var lines = File.ReadAllLines(@"C:/temp/TextFile.txt").Select(x => x.Split('/')).FirstOrDefault();
            var words = new Dictionary<string, string>();

            for (int i = 0; i < lines.Length - 1; i += 2)
            {
                words.Add(lines[i], lines[i + 1]);
                Console.WriteLine(string.Format("{0} - {1}", lines[i], lines[i + 1]));
            }

            //Console.WriteLine(words);
            Console.ReadKey();
        }
    }
}

Here is Scenario 2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace test1
{
    class Program
    {

        static void Main(string[] args)
        {
            var lines = File.ReadAllLines(@"C:/temp/TextFile.txt").Select(x => x.Split('/')).FirstOrDefault();
            var words = new Dictionary<string, string>();

            for (int i = 0; i < lines.Length - 1; i += 2)
            {
                words.Add(lines[i], lines[i + 1]);
            }

            foreach (var kvp in words)
            {
                Console.WriteLine(string.Format("{0} - {1}", kvp.Key, kvp.Value));
            }
            Console.ReadKey();
        }
    }
}

Here is Scenario 3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace test1
{
    class Program
    {

        static void Main(string[] args)
        {
            var lines = File.ReadAllLines(@"C:/temp/TextFile.txt").Select(x => x.Split('/')).FirstOrDefault();
            var words = new Dictionary<string, string>();

            for (int i = 0; i < lines.Length - 1; i += 2)
            {
                words.Add(lines[i], lines[i + 1]);
            }

            PrintDictionary(words);
            Console.ReadKey();
        }

       static void PrintDictionary(Dictionary<string,string> dict)
       {
           foreach (var kvp in dict)
            {
                Console.WriteLine(string.Format("{0} - {1}", kvp.Key, kvp.Value));
            }
       }
    }
}

Upvotes: 1

ProgrammingLlama
ProgrammingLlama

Reputation: 38757

Try this

var words = File.ReadAllLines(@"C:/temp/TextFile.txt")
                .Select(x => x.Split('/'))
                .Where(x => x.Length == 2)
                .ToDictionary(x => x[0], x => x[1]);

It reads the file, and splits it as you currently do. The where simply checks that it's a pair and there aren't more/less elements (to prevent an exception in case of the latter). Finally, it converts it to a dictionary.

I'm not sure what you want to do with regards to writing it out. juharr's suggestion in the comments should work:

Console.WriteLine(string.Join(Environment.NewLine, words.Select(kvp=>kvp.Key + ":" + kvp.Value)));

or you can loop through each pair:

foreach (var pair in words)
{
    Console.WriteLine(pair.Key + ":" + pair.Value);
}

Upvotes: 3

Related Questions