conversion problem

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
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);
                
            }

        }
    }
}
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
What error are you getting?

If you are not getting an error, what behavior are you seeing? What behavior were you expecting to see?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
As an aside, if you are iterating over a collection, you shouldn't modify the collection. So on line 113, you are iterating over the collection tmb2, but on line 121, you are trying to add more items to the collection.
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
I get the following error:
Severity Code Description Project File Line Suppression State
Error CS0019 Operator '==' cannot be applied to operands of type 'string' and 'char' ConsoleApp2karater1 C:\Users\Dell\source\repos\ConsoleApp2karater1\ConsoleApp2karater1\Program.cs 117 Active
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
The error there is spot on. You can't compare a string and a character. It's like trying to compare an apple to a bucket of apples. When you use the indexer operator [ ] on a string, it will return a single character. But your mystruct.kar member is declared as a string as per line 12.
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
thank you for your help. I had a guess about it but my problem is how to convert to match the rules? can I get help? thank you.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,917
Location
Sydney, Australia
Programming Experience
10+
What are you actually trying to do on that line? We can't tell you how to properly write a line of code if we don't know what that line of code is supposed to do. If you stop and think about that for yourself, you'll probably be able to write the correct code yourself but, if you can't, you need to provide us with a full and clear explanation of the problem. The biggest reason I see that people have issues writing code is that they don't think about the specific task that each line or section of code is supposed to perform and so they end up with code not doing what it should.
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
What are you actually trying to do on that line? We can't tell you how to properly write a line of code if we don't know what that line of code is supposed to do. If you stop and think about that for yourself, you'll probably be able to write the correct code yourself but, if you can't, you need to provide us with a full and clear explanation of the problem. The biggest reason I see that people have issues writing code is that they don't think about the specific task that each line or section of code is supposed to perform and so they end up with code not doing what it should.
thank you for your help. I have a collection of characters in a record list and I try to count the same ones in another string, with other words count occurrences. also, I tried this
C#:
int i2 = 0;

for (int dt=0;dt<hossz;dt++)
{
    foreach (mystruct dt2 in tmb2)
    {
        i2++;
        string str = dt2.kar;
        char character = str.ToCharArray()[0];
        mystruct mydata = new mystruct();
        
        if (character == str1[i2])
        {
            mydata.a += 1;
            tmb2.Add(mydata);
        }
    }
}
and this is the error
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
 
Last edited by a moderator:

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
thank you for your help. I have a collection of characters in a record list and I try to count the same ones in another string, with other words count occurrences. also, I tried this
C#:
int i2 = 0;

for (int dt=0;dt<hossz;dt++)
{
    foreach (mystruct dt2 in tmb2)
    {
        i2++;
        string str = dt2.kar;
        char character = str.ToCharArray()[0];
        mystruct mydata = new mystruct();
       
        if (character == str1[i2])
        {
            mydata.a += 1;
            tmb2.Add(mydata);
        }
    }
}
and this is the error
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
thank you
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,917
Location
Sydney, Australia
Programming Experience
10+
That was addressed in post #3. You cannot enumerate a list with a foreach loop and modify the list within the loop. Either you want to modify the list or you don't. If you do, you can't use a foreach loop.
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
That was addressed in post #3. You cannot enumerate a list with a foreach loop and modify the list within the loop. Either you want to modify the list or you don't. If you do, you can't use a foreach loop.
thank you. I understand that part. I have a record list where ' kar ' fields contain the characters to check occurrences in another string and 'a' where comes summed up the particular occurrence. so I am wondering if I should change ' foreach ' loop to for loop and how to do it correctly? thank you.
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
thank you for your help. considering advice given for my problem, I tried the following:
sum up:
for (int dt=0;dt<hossz;dt++)
            {
                //foreach (mystruct dt2 in tmb2)
                for (int dt2 = 0; dt2 < hossz; dt2++)
                {
                    //i2++;
                    mystruct mydata = new mystruct();
                    string str = mydata.kar;
                    char character = str[0];
                    
                    if (character == str1[i2])
                    {                         
                        mydata.a += 1;
                        tmb2.Add(mydata);
                    }
                }

            }
the error is :
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I was using StackOverflow for learning, I don't understand it. please help me.thank you
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
thank you for your help. considering advice given for my problem, I tried the following:
sum up:
for (int dt=0;dt<hossz;dt++)
            {
                //foreach (mystruct dt2 in tmb2)
                for (int dt2 = 0; dt2 < hossz; dt2++)
                {
                    //i2++;
                    mystruct mydata = new mystruct();
                    string str = mydata.kar;
                    char character = str[0];
                   
                    if (character == str1[i2])
                    {                        
                        mydata.a += 1;
                        tmb2.Add(mydata);
                    }
                }

            }
the error is :
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I was using StackOverflow for learning, I don't understand it. please help me.thank you
I am sorry for duplication, as I restarted my pc, I feel I've lost all my threads. regarding my question I changed the loop and still data type issue persists. I think this time comparison should happen conform my guess char to char and it doesn't. can I get help? thank you.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
We aren't sitting beside you. We can't see what code you've written or changed. Post your updated code, and tell us what error(s) you are facing. If you are going to present more than 25 lines of code, tell us what line(s) are showing the error(s).

Help us help you. Just giving vague descriptions, or giving us code dumps without explanations does help us. It just make it harder to try to help you because we need to expend more effort. Considering that we are volunteers on this forum, a lot of us will just expend our efforts elsewhere.
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
thank you for help. conform previous advice, indications, I changed the foreach loop to for loop and I tried to sort out the character-string issue but I couldn't . the code
count char:
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);


            }
          
            /* */
            int i2 = 0;
            for (int dt=0;dt<hossz;dt++)
            {
                //foreach (mystruct dt2 in tmb2)
                for (int dt2 = 0; dt2 < hossz; dt2++)
                {
                    //i2++;
                    mystruct mydata = new mystruct();
                    string str = mydata.kar;
                    char character = str[0];
                    
                    if (character == 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);
                
            }

        }
    }
}
the faulty part is
faulty:
char character = str[0];
                    
                    if (character == str1[i2])
                    {                         
                        mydata.a += 1;
                        tmb2.Add(mydata);
                    }
I don't understand tghe fault.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
thank you
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
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.
 

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
60
Programming Experience
Beginner
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
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
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?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,381
Location
Chesapeake, VA
Programming Experience
10+
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."
}
 
Top Bottom