conversion problem

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
71
Programming Experience
Beginner
Hi
thank you for helping me previously. at the moment I am facing a conversion issue. I managed to get the list of characters to check its characters against another string to count the same characters. I need help in this matter.
the faulty part is :
if (dt2.kar == str1[i2])
{

mydata.a += 1;
tmb2.Add(mydata);
}
I can see the error but I need help to correct it to make this app. running.
thank you.

data type conversion:
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp2karater1
{
    class Program
    {
        class mystruct{
          public string kar;
          public int a;
        }
        static void Main(string[] args)
        {
            int[] a = new int[14];
            int i = 0;
            int j = 0;
            int k = -1;
            int hossz = 0;
            string[] tomb = new string[14];
            List<mystruct> tmb2 = new List<mystruct>();
            int[] tomb2 = new int[14];
            string str1 = "hello world!?/";
            string str2 = "";
            mystruct[] tmb = new mystruct[14];
          

            hossz = str1.Length;
            mystruct myrek = new mystruct();

            for (i = 0; i < hossz; i++)
            {
                for (j = 0; j < hossz; j++)
                {
                    if (str1[j] == str1[i])
                    {

                        a[j] = a[j] + 1;
                        if (a[j] < 2)
                        {
                            //k = k + 1;
                            tomb[i] = Convert.ToString(str1[i]);
                            tomb2[i] = 1;
                            k = k + tomb2[i];
                            
                            //tmb.kar[i]=str1[i];
                            //str2+=str1[i];
                        }
                        else
                        {
                            tomb[i] = "empty";
                        }



                    }

                }
                Console.Write(a[i] + " i " + i + " k " + k + " " + tomb[i] + " " + tomb2[i] + " \n");

            }

            k = 0;
            for (i = 0; i < hossz; i++)
            {
                tomb2[i] = tomb2[i] + 1;
                Console.Write(" a[" + i + " ] " + a[i] + " " + tomb[i]);
                Console.Write(" " + tomb2[i] + "\n");
                if (tomb[i] != "empty")
                {
                    mystruct mydata = new mystruct();
                    str2 += tomb[i];
                    mydata.kar +=tomb[i];
                    tmb2.Add(mydata);
                }

            }
            foreach (char ch in str2)
            {
                Console.WriteLine(" ch " + ch);
              

            }
            foreach (mystruct ch2 in tmb2)
            {
                Console.WriteLine(" tmb 1 " + ch2.kar);


            }
            /*
                
            for (int i2 = 0;i2<14;i2++)
            {
                
                //tmb[i2].kar = str2[i2];
                   if (tomb[i2] != "empty")
                   {

                    //tmb2[i2].kar += tomb[i2];
                    //tmb[i].kar += tomb[i];
                    Console.WriteLine(" tmb " + tmb2[i2].kar);
                }
                


            }*/
            /* */
            int i2 = 0;
            for (int dt=0;dt<hossz;dt++)
            {
                foreach (mystruct dt2 in tmb2)
                {
                    i2++;
                    mystruct mydata = new mystruct();
                    if (dt2.kar == str1[i2])
                    {
                        
                        mydata.a += 1;
                        tmb2.Add(mydata);
                    }
                }

            }
            int i3 = -1;
            foreach (mystruct rek in tmb2)
            {
                i3++;
                
                    Console.WriteLine(i3 + " elem " + rek.kar + " db " + rek.a);
                
            }

        }
    }
}
 
Yes, you are getting a null reference exception because str is null due to line 102. Your mystruct that you created on line 101 contains a null reference for the mydata.kar member because you don't do anything to initialize it.
 
Thank you for help. now the data type match problems looks solved but counting doesn't count however it should. it looks very same as a few lines above where I select the unic characters.
count:
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp2karater1
{
    class Program
    {
        class mystruct{
          public string kar=" ";
          public int a=0;
        }
        static void Main(string[] args)
        {
            int[] a = new int[14];
            int[] a2 = new int[14];
            int i = 0;
            int j = 0;
            int k = -1;
            int hossz = 0;
            string[] tomb = new string[14];
            List<mystruct> tmb2 = new List<mystruct>();
            int[] tomb2 = new int[14];
            string str1 = "hello world!?/";
            string str2 = "";
            mystruct[] tmb = new mystruct[14];
          

            hossz = str1.Length;
            mystruct myrek = new mystruct();

            for (i = 0; i < hossz; i++)
            {
                for (j = 0; j < hossz; j++)
                {
                    if (str1[j] == str1[i])
                    {

                        a[j] = a[j] + 1;
                        if (a[j] < 2)
                        {
                            //k = k + 1;
                            tomb[i] = Convert.ToString(str1[i]);
                            tomb2[i] = 1;
                            k = k + tomb2[i];
                            
                            //tmb.kar[i]=str1[i];
                            //str2+=str1[i];
                        }
                        else
                        {
                            tomb[i] = "empty";
                        }



                    }

                }
                Console.Write(a[i] + " i " + i + " k " + k + " " + tomb[i] + " " + tomb2[i] + " \n");

            }

            k = 0;
            int hossz2 = -1;
            for (i = 0; i < hossz; i++)
            {
                tomb2[i] = tomb2[i] + 1;
                Console.Write(" a[" + i + " ] " + a[i] + " " + tomb[i]);
                Console.Write(" " + tomb2[i] + "\n");
                if (tomb[i] != "empty")
                {
                    mystruct mydata = new mystruct();
                    str2 += tomb[i];
                    mydata.kar +=tomb[i];
                    tmb2.Add(mydata);
                    
                }

            }
            //int hossz2 = tmb2.Length;
            foreach (char ch in str2)
            {
                Console.WriteLine(" ch " + ch);
              

            }
            int i2 = 0;
            foreach (mystruct ch2 in tmb2)
            {
                i2++;
                hossz2 += 1;
                Console.WriteLine(" tmb "+hossz2+" " + ch2.kar);


            }
          
            /* */
            
            for (int dt=0;dt<hossz2;dt++)
            {
                
                //foreach (mystruct dt2 in tmb2)
                for (int dt2 = 0; dt2 < hossz; dt2++)
                {
                    //i2++;
                    
                    //string str = mydata.kar;
                    //char character = str[0];
                    string character = tmb2[dt].kar;
                    
                    if (character == str1[dt2].ToString())
                    {
                        a2[dt2] += 1;
                        tmb2[dt].a = a2[dt2];
                        
                    }
                    Console.WriteLine(" tmb2 " + dt + " " + tmb2[dt].kar + " a2 " + tmb2[dt].a);

                }
                mystruct mydata = new mystruct();
                mydata.a = a2[dt];
                tmb2.Add(mydata);
                Console.WriteLine(" hossz " + dt+ " " + tmb2[dt].kar + " " + tmb2[dt].a);
            }

            int i3 = -1;
            foreach (mystruct rek in tmb2)
            {
                i3++;
                
                    Console.WriteLine(i3 + " elem " + rek.kar + " db " + rek.a);
                
            }
            i3 = 0;
        }
    }
}
the faulty part
faulty counter:
for (int dt=0;dt<hossz2;dt++)
            {
                
                //foreach (mystruct dt2 in tmb2)
                for (int dt2 = 0; dt2 < hossz; dt2++)
                {
                    //i2++;
                    
                    //string str = mydata.kar;
                    //char character = str[0];
                    string character = tmb2[dt].kar;
                    
                    if (character == str1[dt2].ToString())
                    {
                        a2[dt2] += 1;
                        tmb2[dt].a = a2[dt2];
                        
                    }
                    Console.WriteLine(" tmb2 " + dt + " " + tmb2[dt].kar + " a2 " + tmb2[dt].a);

                }
                mystruct mydata = new mystruct();
                mydata.a = a2[dt];
                tmb2.Add(mydata);
                Console.WriteLine(" hossz " + dt+ " " + tmb2[dt].kar + " " + tmb2[dt].a);
            }
thank you
 
What inputs are you giving?
What output are you getting?
What output were you expecting to get?
What have you done to try to narrow down the problems?
What have you done to try to fix the problems?
 
As as aside, in my opinion, part of what is making things harder for yourself (as well as others like us reading your code) is your use of kar to hold a character, but you have declared it to be a type string instead of type char. And then there's the use of the name a to hold the count for that character. Why not use a more meaningful name?

Well named variables, classes/structs, and methods make understanding code much, much, easier. (It's why the first thing that code obfuscators do is mangle the names of things during the first pass.) Understandably, it looks like we have a language barrier, so a meaningful name to you in your language will look like gibberish to us English readers, but it might at least get you over the hump of understanding your own code and seeing how it works.

As an aside, are you not allowed to use the Dictionary<> class? I can see that you are using the List<> so it seems like you are allowed to use some of the built in data structures within the .NET Framework.

With a dictionary, then your problem can be easily solved. Here's the pseudo code:
C#:
var characterCounts = new Dictionary<char, int>();

foreach (char ch in firstString)
{
    if (characterCounts contains ch as a key)
        characterCounts[ch]++
    else
        characterCounts[ch] = 1
}

foreach (char ch in secondString)
{
    if (characterCounts contains ch as a key)
        print ch + " appears " + characterCounts[ch] + " time(s) in the  first string."
    else
        print ch + " does not appear in the first string."
}
 
Back
Top Bottom