Click here to Skip to main content
15,887,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
string line1 = "Sam";
           string line2 = "matches";
           string line3 = "John";
           string line = line1 + line2 + line3;
           int[] numbers = new int[1000];

           string[] words = new string[100];
           words[0] = line;

           Dictionary<char, int> map = new Dictionary<char, int>();
           foreach (string s in words)
           {
               foreach (char c in s.ToArray())
               {
                   if (!map.ContainsKey(c))
                   {
                       map.Add(c, 1);
                   }
                   else
                   {
                       map[c] = map[c] + 1;
                   }

               }
               foreach (var item in map.Keys)
               {
                   numbers[item] = map[item];
               }
               foreach (var mapitem in map.Keys)
               {
                   Console.WriteLine($"'{mapitem}' repeats {map[mapitem]} time(s)");
                   Console.ReadLine();
               }

           }
           for (int i =0; i< line.Length;i++)
           {
               Console.WriteLine(numbers[i]);
           }

       }
   }


What I have tried:

I have tried the above code. It displays the number of duplicates but I keep getting an error that points to ToArray and it doesnt not go to displaying the array.
Posted
Updated 3-Oct-21 23:22pm
Comments
Richard MacCutchan 4-Oct-21 4:51am    
What error?
CoderL18 4-Oct-21 5:05am    
It points to s.ToArray() -> And shows this: An unhandled exception of type 'System.ArgumentNullException' occurred in System.Core.dll

Additional information: Value cannot be null.

The correct method for a string is .ToCharArray; .ToArray is a LINQ extension method, which requires a using System.Linq; statement at the top of your file, and will be less efficient than the ToCharArray method.

String.ToCharArray Method (System) | Microsoft Docs[^]
Enumerable.ToArray<TSource>(IEnumerable<TSource>) Method (System.Linq) | Microsoft Docs[^]

However, you don't need to use either method to enumerate the characters in a string, since String already implements IEnumerable<char>:
C#
foreach (char c in s)
{
    ...

NB: You're going to get a NullReferenceException, since you're iterating over an array of strings which haven't been initialized. Every string in that array except for the first will be null.
C#
foreach (string s in words)
{
    if (s == null) continue;
    
    foreach (char c in s)
    {
        ...
 
Share this answer
 
v3
Comments
CoderL18 4-Oct-21 5:14am    
I have tried that but then it points to the "in" in the foreach and gives me this error: An unhandled exception of type 'System.NullReferenceException' occurred in GoodMatch.exe

Additional information: Object reference not set to an instance of an object.
Richard Deeming 4-Oct-21 5:24am    
You've created an array of 100 strings, but you've only initialized the first element of that array. The other 99 elements will be null, so you will get a NullReferenceException.
CoderL18 4-Oct-21 5:29am    
Oh yes I forgot I had initialized it to 100 instead of 1. Thank you so much!.
Quote:
An unhandled exception of type 'System.NullReferenceException' occurred in GoodMatch.exe

Additional information: Object reference not set to an instance of an object.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
CoderL18 4-Oct-21 5:30am    
You don't have to be so rude !, We all are still learning and we all make mistakes, nobody is prefect.
Phil J Pearson 4-Oct-21 6:11am    
OG was not in the least rude! He was being very patient and giving you good, helpful advice. Do yourself a favour: read it and take note.
OriginalGriff 4-Oct-21 6:15am    
I'm sorry if you thought I was being rude, that was not my intention.

Could you please indicate what part of my message gave offence and explain why so I can endeavour to avoid making the same mistake in future. This site has a world-wide membership, and it's sometimes difficult to know what will offend in a different local culture, particularly when you have no idea what country the other person resides in, much less what sub-section of that culture he comes from. And using the default "USA" location doesn't help us with that: I can't see anything in that message that should offend an American!

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