Page 1 of 1

2 Replies - 463 Views - Last Post: 04 May 2012 - 12:50 PM

#1 Gonzo0193

Reputation: 2
• Posts: 49
• Joined: 16-December 09

Posted 03 May 2012 - 02:51 PM

I am loading maps into an array from a text file using this code:

```            using (StreamReader sr = new StreamReader(layerFile))
{
string line;
int[,] partsArray = new int[(int)mapsize.X, (int)mapsize.Y];
int partCounter = 0;
int lineCounter = 0;
while ((line = sr.ReadLine()) != null)
{
string[] parts = line.Split(',');

//build the parts array
for (int rowWidthCounter = 0; rowWidthCounter < line.Length/2; rowWidthCounter++)
{
partsArray[lineCounter, rowWidthCounter] = Convert.ToInt32(parts[partCounter]);
partCounter++;
}
lineCounter++;
partCounter = 0;
}
//layer1 = partsArray;
holderLayer = partsArray;

switch (layerNum)
{
case 1: layer1 = holderLayer; break;
case 2: layer2 = holderLayer; break;
default: break;
}

}

```

```1,1,1,1,1,1,1,1,1,1,1,
1,1,1,6,7,1,1,1,1,1,1,
1,1,1,8,9,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,

```

As long as the number between ,'s is 0 - 9 then it all works fine, i can even add a double digit number to a line, but if i try to add two double didgit numbers to a line like:

```1,1,1,1,1,1,1,1,1,1,1,
1,1,1,6,7,1,1,1,1,1,1,
1,1,1,8,9,1,1,1,1,1,1,
1,1,1,10,11,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,

```

then i get this error:

```A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Input string was not in a correct format.

```

and i have no idea why. =[

Is This A Good Question/Topic? 0

#2 lesPaul456

Reputation: 174
• Posts: 729
• Joined: 16-April 09

Posted 03 May 2012 - 04:59 PM

It's a pretty simple solution. The problem lies is with your for loop. This part in particular: rowWidthCounter < line.Length/2.

I understand what you're trying to do here, but it simply won't work with multiple digits.

```// ...

for (int rowWidthCounter = 0; rowWidthCounter < mapSize.Y; rowWidthCounter++)
{
partsArray[lineCounter, rowWidthCounter] = Convert.ToInt32(parts[partCounter]);
partCounter++;
}

// ...

```

Notice that I changed the conditional-expression to this: rowWidthCounter < mapSize.Y

Hope this helps point you in the right direction!

#3 Gonzo0193

Reputation: 2
• Posts: 49
• Joined: 16-December 09