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);
                
            }

        }
    }
}
 
What error are you getting?

If you are not getting an error, what behavior are you seeing? What behavior were you expecting to see?
 
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.
 
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
 
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.
 
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.
 
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.
 
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:
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
 
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.
 
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.
 
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
 
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.
 
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.
 
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
 
Back
Top Bottom