Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a dictionary where the compiler reads from a text file where there are two words per line. I have parsed it through the split() method but I am struggling on how to add the corresponding keys and values from the line to the dictionary container. I am trying to add it in the ReadStream2() function after doing the split() in the line line.add(words[0],words[1]). I know this is wrong but I have no idea how to combine what I am parsing into the dictionary in terms of keys and values.Also on the part where I write the askWord() function I am trying to state that if a line key matches with user input it will output the value(translation) of the word. But the line object of the dictionary I created does not pick up keys and values. Thanks!

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

namespace Project_Dictionary
{
    class Program
    {


        static void Main(string[] args)
        {
            Dictionary<string, string> line = new Dictionary<string, string>();
            FileStream filestream = null;
            string path = "Dictionary.txt";
            //WriteByte(filestream, path);
            //ReadByte(filestream, path);
            //WriteStream(filestream, path);
            //ReadFromFile();
            Menu(filestream, path);
            ReadStream2(filestream,path);
            
            Group(filestream, path);
            
        }
       
        static void WriteByte(FileStream filestream, string path)
        {
            string str;
            
            Console.WriteLine("Enter word");
            str = Console.ReadLine();
            try
            {
                filestream = new FileStream("Dictionary.txt", FileMode.Open, FileAccess.Write);
                byte[] by = Encoding.Default.GetBytes(str);
                filestream.Write(by, 0, by.Length);
                Console.WriteLine("File written");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                filestream.Close();
            }
        }
        static void ReadByte(FileStream filestream, string path)
        {

            try
            {
                filestream = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[] by = new byte[(int)filestream.Length];
                filestream.Read(by, 0, by.Length);
                string str = Encoding.Default.GetString(by);
                Console.WriteLine("File read");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                filestream.Close();
            }
        }
        static void WriteStream(FileStream filestream, string path)
        {
            using (filestream = new FileStream(path, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter streamWriter = new StreamWriter(filestream))
                {
                    //string str;
                    //Console.WriteLine("Enter word");
                    //str = Console.ReadLine();
                    //streamWriter.WriteLine(str);
                }
            }
        }
        static void ReadStream2(FileStream fileStream, string path)
        {
            using (fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                Dictionary<string, string> line = new Dictionary<string, string>();
                using (StreamReader sw = new StreamReader(fileStream))
                {
                    string rez = "";
                    
                    while(sw.Peek() > 0)
                    {
                        rez = sw.ReadLine();
                        
                        Console.WriteLine(rez);
                        string[] words = rez.Split(' ');
                        line.Add(words[0], words[1]);
                       
                    }
                
                    
                    
                }
            }
        }
        static void Group(FileStream fileStream, string path)
        {
            var line = File
            .ReadLines(path)
            .Select((v, i) => new { Index = i, Value = v })
            .GroupBy(p => p.Index / 2)
            .ToDictionary(g => g.First().Value, g => g.Last().Value);
        }
        static void Menu(FileStream fileStream, string path)
        {
            char choice;
            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine("Welcome this is a English-Ukrainian dictionary press d to continue");
            Console.ResetColor();
            choice = Convert.ToChar(Console.ReadLine());
            while (choice == 'd' || choice == 'D')
            {
                ReadStream2(fileStream, path);
            }
        }
        static void askWord()
        {
            string ask;
            Console.WriteLine("What english word would you like to translate");
            ask = Console.ReadLine();
            if (ask == )
        }

    }
}


What I have tried:

I have tried combining it all but I do not know how to proceed in tying it into the dictionary container and then extracting the corresponding keys and values.
Posted
Updated 31-Oct-22 4:00am

1 solution

Maybe you can use something like in this example from:
dictionary-if-key-exist-append-if-not-add-new-element-c-sharp[^]

private Dictionary<string, List<string>> dictionary;

void AddCountries(string languageKey, List<string> coutriesToAdd)
{
    List<string> existingValue = null;

    if (!dictionary.TryGetValue(languageKey, out existingValue))
    {
        // Create if not exists in dictionary
        existingValue = dictionary[languageKey] = new List<string>()
    }

    existingValue.AddRange(coutriesToAdd);
}


This solves the problem of having multiple values for a key.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900