Jump to content

Parsing a Line and putting it to array error

By dashker
in Miscellaneous

Recommended Posts

try
                    {
                        string ReadedLine = sr.ReadLine();
                        int resultingname = int.Parse(ReadedLine.Split(',')[0]);

                        if (race == resultingname)
                        {
                            Found = true;
                            for (int i = 0; i < 69; i++)
                            {
                                strvector[i] = ReadedLine.Split(',')[i];
                            }
                        }
                    }
                    catch (System.FormatException)
                    {
                        MessageBox.Show("Fuck You!");
                    }

When I do this i am having continuosly System.FormatError and i don't know Why :(

Thansk for the help

Link to comment
Share on other sites

Try replacing your while clause with the below. I think the problem is that the first field of your csv file isn't an integer and this is where it is breaking - by default DBCUtil adds a header row of column types. To prevent this I've replaced it with TryParse which returns a boolean if it is successful and stores the parsed integer in the resultingname variable.

I've also stored the columns in an array to prevent having to call Split more than once and also to prevent a possible array overflow in your for loop (it should never do so as the dbc file should always have 70 columns).

while (!Found && sr.Peek() != -1)
            {
                string ReadedLine = sr.ReadLine();
                string[] fields = ReadedLine.Split(',');
                int resultingname = 0;
                
                if (int.TryParse(fields[0], out resultingname) && race == resultingname)
                {
                    Found = true;
                    for (int i = 0; i < fields.Length; i++)
                        strvector[i] = fields[i];
                }
            }

 

Link to comment
Share on other sites

9 hours ago, barncastle said:

Try replacing your while clause with the below. I think the problem is that the first field of your csv file isn't an integer and this is where it is breaking - by default DBCUtil adds a header row of column types. To prevent this I've replaced it with TryParse which returns a boolean if it is successful and stores the parsed integer in the resultingname variable.

I've also stored the columns in an array to prevent having to call Split more than once and also to prevent a possible array overflow in your for loop (it should never do so as the dbc file should always have 70 columns).


while (!Found && sr.Peek() != -1)
            {
                string ReadedLine = sr.ReadLine();
                string[] fields = ReadedLine.Split(',');
                int resultingname = 0;
                
                if (int.TryParse(fields[0], out resultingname) && race == resultingname)
                {
                    Found = true;
                    for (int i = 0; i < fields.Length; i++)
                        strvector[i] = fields[i];
                }
            }

 

How i can store it into an array, i need to do an array or a matrix, and how i get the length of a line?

Link to comment
Share on other sites

4 minutes ago, Thoraric said:

The feeling when you open a new post in the hope of learn something new but you don't understand anything. :D

i am understanding it, but never used this things, and minus in class, so i need help to learn, i think it's normal...

Link to comment
Share on other sites

I might be missing something but your current code will only return one record and stores it in strvector which is already an array? If you want to parse the entire CSV you'll need something like a list or dictionary and add strvector to it each time it reads a line - you'll need to remove the Found check for that.

To get column count you could do something like ReadedLine.Split(',').Length. To get the a row's field count do the same on strvector i.e. strvector.Length.

Link to comment
Share on other sites

×
×
  • Create New...