0

I have date going in to a text file, form the variable nameSplit. The following code is checking to see if the string that has come from the text file to nameSplit had previously been saved to the dictionary. If so, then it prints "Hoozah!", else it adds the data to the array and prints "Failure". However, if I enter the name "Jim" 5 times in to the text file, it just outputs `Failure", although it should have outputted "hoozah!" on the second entry. I'm not sure what is wrong!

while (!sR.EndOfStream) { string line = sR.ReadLine(); string split = line.Split('$', ',')[1]; string nameSplit = line.Split('.', ':')[1]; Dictionary<string, decimal> names = new Dictionary<string, decimal>(); // inside the loop decimal n = Convert.ToDecimal(split); if (names.ContainsKey(nameSplit)) { names[nameSplit] += n; names.Add(nameSplit, n); Console.WriteLine("Hoozah!"); } else { names.Add(nameSplit, n); Console.WriteLine("Failure"); } } sR.Close(); GC.Collect(); Thread.Sleep(66); 

DATA:

  1. Jim: $22.00, 2. Jim: $2100.00, 3. Jim: $6.00, 4. Jim: $32.00, 5. Jim: $2.00,
5
  • 3
    Did you forget to put the code ? Commented Nov 17, 2014 at 20:10
  • 2
    A good time to get to know the debugger. The bug is easy to find with it. Commented Nov 17, 2014 at 20:13
  • I've never used a debugger before :/ How does it work Commented Nov 17, 2014 at 20:14
  • 2
    And get rid of the GC.Collect() there at the end. You almost never need to deal directly with the garbage collector. Commented Nov 17, 2014 at 20:14
  • Done, thanks for that, sorry I'm kinda new to programming Commented Nov 17, 2014 at 20:16

1 Answer 1

7

Define the dictionary outside of the loop. Right now you are just creating a new dictionary on each loop and basically forgetting results from the previous loop.

Dictionary<string, decimal> names = new Dictionary<string, decimal>(); while (!sR.EndOfStream) { ... } 

EDIT

Based on your data you actually need to split up the "entries" on each line and loop through them. Assuming that there are multiple lines with a similar format you'll want to do the following.

Dictionary<string, decimal> names = new Dictionary<string, decimal>(); while (!sR.EndOfStream) { string line = sR.ReadLine(); var entries = line.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries); foreach(var entry in entries) { decimal amount = decimal.Parse(entry.Split('$')[1]); string nameSplit = entry.Split('.', ':')[1].Trim(); if (names.ContainsKey(nameSplit)) { names[nameSplit] += amount; Console.WriteLine("Hoozah!"); } else { names.Add(nameSplit, amount); Console.WriteLine("Failure"); } } } 

Note that I renamed some variables and trimmed white space from the name, but the main thing is to split on the commas and iterate over each "entry". I'm not sure why you are keeping a running sum of the dollar amounts, but you could use those after the loop.

Sign up to request clarification or add additional context in comments.

17 Comments

I have done that, but it is still just printing failure
@ETSTimeLapse Are you sure the nameSplit values are the same? Can you show us the input in your question?
What do you mean? sorry
@ETSTimeLapse Can you post the lines of data that the sR stream is reading, so we can see if there is an issue in the data? For instance if one line is "blah:Jim" and another is "blah: Jim" they will not match because of the space. We need to see the data you are processing to give you further help with your question.
Txt file is formatted as so 1. dddd: $21312.00, 2. dddd: $2131.00, 3. dddd: $21.00, 4. asdfddsdddddd: $3232.00, 5. sdfasdf: $21.00,
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.