0

I need to resolve this homework but I am stuck reading the Tkey and returning the Tvalue. Could somebody help me ? Please....

//Create a dictionary list of employee IDs and the name that goes with that IDs.Fill it with a few
//records.then ask the user for their ID and return the name.

Dictionary<int, string> listOfemployees = new Dictionary<int, string>();

listOfemployees[1050] = "Juan Trejo";
listOfemployees[1060] = "Esmeralda Trejo";
listOfemployees[1070] = "Danilo Martinez";
listOfemployees[1080] = "Jorge Ortiz";
listOfemployees[1090] = "Julio Algo";

Console.Write("What is your id");
Console.Readline(); // this is my problem reading the id Tkey and returning Tvalue. 
001
  • 13,291
  • 5
  • 35
  • 66
  • 1
    What are you having problems with? Turning what the user enters into an `int` or retrieving the value from the dictionary? It should just be `listOfemployees[id]` to get the value once you get the ID from the user. – itsme86 Aug 10 '20 at 16:16
  • You should provide more info, to help you!! what actually you want to do ? – Doom Aug 10 '20 at 16:21
  • 1
    `int id = Int32.Parse(Console.Readline()); Console.WriteLine(listOfemployees[id]);` – 001 Aug 10 '20 at 16:21
  • Thank you Johnny. – Claudio Aug 10 '20 at 17:51

2 Answers2

0

To get an int from the user: Reading an integer from user input

And then access the dictionary like this:

int intFromUser = ...;
string stringFromDict = listOfemployees[intFromUser];
Fumeaux
  • 36
  • 1
  • 9
0
Dictionary<int, string> listOfemployees = new Dictionary<int, string>();

        listOfemployees[1050] = "Juan Trejo";
        listOfemployees[1060] = "Esmeralda Trejo";
        listOfemployees[1070] = "Danilo Martinez";
        listOfemployees[1080] = "Jorge Ortiz";
        listOfemployees[1090] = "Julio Algo";

        Console.WriteLine("What is your id");
        // reading user input and converting it to int.
        int id = int.Parse(Console.ReadLine());

        //printing what's inside dictionary based on user's input
        Console.Write(listOfemployees[id]);
Doom
  • 206
  • 1
  • 4
  • 14