简体   繁体   English

C#字典添加后找不到键

[英]C# Dictionary doesn't find the key after being added

I've made a simple program that will read functions in a text file splitted by a new line and execute it just like a programming language. 我编写了一个简单的程序,该程序将读取由新行分隔的文本文件中的功能,并像编程语言一样执行它。 Here's the code: 这是代码:

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

namespace My_Project
{
class Program
{
    public static Dictionary<string, string> variables = new Dictionary<string, string>();
    static void Main(string[] args)
    {            
        //string code = args[0];
        string code = File.ReadAllText(@"TEXT FILE LOCATION HERE");
        var lines = code.Split('\n');
        foreach (string line in lines)
        {
            char[] lineInCharArray = line.ToCharArray();
            if (lineInCharArray[0] == '-')
            {
                string variablename = (lineInCharArray[1]).ToString();
                string value = line.Replace("-" + variablename, "");
                Console.WriteLine("VARIABLE DEFINED: " + variablename + " - " + value);
                variables.Add(variablename, value);
            }
            if (lineInCharArray[0] == '*')
            {
                if (lineInCharArray[1] == '*')
                {
                    try
                    {
                        string requestedVariable = line.Replace("**", "");
                        Console.WriteLine("REQUESTED VARIABLE: " + 
requestedVariable);
                        string printedValue = variables[requestedVariable];
                        Console.WriteLine(printedValue);
                    }
                    catch
                    {
                        Console.WriteLine("ERROR: ");
                        List<string> hhh = variables.Keys.ToList();
                        foreach (string item in hhh)
                        {
                            Console.WriteLine("--: " + item);
                        }
                    }
                }
                else
                {
                    string printedValue = line.Replace("*", "");
                    Console.WriteLine(printedValue);
                }
            }
            if (lineInCharArray[0] == '+')
            {
                string variable = variables[lineInCharArray[1].ToString()];
                Int32 variableInInt = Convert.ToInt32(variable);
                Int32 incrementation = Convert.ToInt32(line.Replace("+" + variable, ""));
                for (int i = 0; i < incrementation + 1; i++)
                {
                    variableInInt++;
                }
                variables[lineInCharArray[1].ToString()] = variableInInt.ToString();
            }
        }
        Console.ReadLine();
    }
}
}

Input (text file) 输入(文本文件)

-a12 // Set a variable named 'a' and set the value to 12
-j8  // Set a variable named 'j' and set the value to 8
**j // Print the value of the variable of 'j'
**a // Print the value of the variable of 'a'

Problem: 问题:

The code outputs the following: 该代码输出以下内容:

VARIABLE DEFINED: a - 12
VARIABLE DEFINED: j - 8
REQUESTED VARIABLE: j
ERROR:
--: a
--: j
REQUESTED VARIABLE: a
12

The code successfully assigned a to 12 and j to 8 , but fails to print j ( a is fine though). 该代码成功地将a分配给a 12,将j分配给a 8 ,但是无法打印j (尽管a很好)。 It says that the key j wasn't found on the dictionary even though the program did add j . 它说,关键j不上即使该程序确实添加了字典中找到j The solution should output the following: 该解决方案应输出以下内容:

VARIABLE DEFINED: a - 12
VARIABLE DEFINED: j - 8
REQUESTED VARIABLE: j
8
REQUESTED VARIABLE: a
12

After the question I asked in a comment, and the answer you posted in a comment, it is obvious that what you have is a line feed character ( \\r ) at the end of j , so what appears to be "j" is not exactly "j" ; 在我在评论中提出的问题以及您在评论中发布的答案之后,很明显,您所拥有的是在j末尾的换行符( \\r ),因此看起来像"j"的不是正好是"j" instead, it is "j\\r" . 相反,它是"j\\r"

So, you need to clean each line that you read from your file from control characters. 因此,您需要从控制字符中清除从文件中读取的每一行。 In fact, you should clean them from any character which is not part of a valid identifier. 实际上,您应该清除不属于有效标识符的任何字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM