0

So I've been looking around for quite a while to try and find a solution to this. I've been trying to make a card game and am stuck at a section in which I create a cad along with its properties. i decided to make it in the form of an array. The code looks like:

(not sure what happened with these first ones)

string[] dogs = System.IO.File.ReadAllLines(@"C:\Users\corin\Documents\C# coding\dogs.txt");

int individual = totalCards / 2;

Random r = new Random();
int Cards = totalCards / 2;

List<List<int>> playerCards = new List<List<int>>(Cards);

for (int x = 0; x < (Cards-2); x++)
{
    playerCards[0].Add(Int32.Parse(dogs[x]));//Cards
    playerCards[1].Add(r.Next(1, 6));//Drool
    playerCards[2].Add(r.Next(1, 101));//Exercise
    playerCards[3].Add(r.Next(1, 11));//Intelligence
    playerCards[4].Add(r.Next(1, 11));//Friendliness
}

No errors are raised before I run the code but when I try it an Argument out of range exception occurs for the line: playerCards[0].Add(Int32.Parse(dogs[x])); I tried removing it and the same error occured for the next line. I'm not sure what I've done wrong and have tried to find a solution for quite some time. If anyone has any tips or answers that would be great. Thanks

Steve
  • 213,761
  • 22
  • 232
  • 286
C.Anderson
  • 11
  • 1
  • 2
    You never add any items in `playerCards`, thus there is no in range index you can read from. – user4003407 Feb 15 '19 at 22:22
  • @AlexeiLevenkov, I believe it's not a duplicate (at least not of that question). In this question there was an attempt to set a capacity, but there is a misunderstanding of what `new List>(Cards)` does. tl;dr: this question covers issue that other question doesn't. –  Feb 15 '19 at 22:48
  • @dyukha I don't think implying that OP can't read documentation is nice. You should assume they did they research prior asking (and checked documentation as part of it). Presumably they've read [`new List(capacity)` initializes … that is empty](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.-ctor?view=netframework-4.7.2#System_Collections_Generic_List_1__ctor_System_Int32_) so they know that list is empty - probably they expect JavaScript behavior which would make your answer unrelated. – Alexei Levenkov Feb 15 '19 at 22:58
  • @AlexeiLevenkov, I personally sometimes misunderstand documentation, so I don't think it's offensive to say that someone misunderstood documentation. In any case, my point is that someone else may make the same mistake. They will search SO for answer, and answers to that question don't provide explanation, while to this one do. Don't misunderstand, I'm the first in the list persons who hate duplicates, I just think this question covers something new. –  Feb 15 '19 at 23:06
  • @dyukha - you are welcome to [edit] question to clarify that this is not duplicate or ask new question that clearly asks about that (you obviously would do your own research to see if anyone already asked about list capacity and initialization). So far OP did not ask what you claiming they asked and "How to solve argument out of range exceptions" is clearly answered in the original duplicate (I've also added one that covers capacity just in case) – Alexei Levenkov Feb 15 '19 at 23:46

2 Answers2

1

try this :

string[] dogs = System.IO.File.ReadAllLines(@"C:\Users\corin\Documents\C# coding\dogs.txt");

int individual = totalCards / 2;

Random r = new Random();
int Cards = totalCards / 2;

List<List<int>> playerCards = new List<List<int>>();

//the missing piece
for (int i = 0; i < (Cards ); i++)
{
     playerCards.add(new List<int>()); 
}


for (int x = 0; x < (Cards-2); x++)
{
    playerCards[0].Add(Int32.Parse(dogs[x]));//Cards
    playerCards[1].Add(r.Next(1, 6));//Drool
    playerCards[2].Add(r.Next(1, 101));//Exercise
    playerCards[3].Add(r.Next(1, 11));//Intelligence
    playerCards[4].Add(r.Next(1, 11));//Friendliness
}
Gilad Guy
  • 11
  • 2
  • 1
    Hi, when you post the answer, it's usually not a good idea to post only the code. You should explain the intuition behind your solution (in more or less plain English). Why the code provided by OP doesn't work and why your works. This way the solution is more clear to OP and to everyone who finds this question by google-search: instead of comparing two peaces of code the can just read a description and understand the solution. –  Feb 15 '19 at 22:44
  • @dyukha I assume that they know that question is already answered many times and likely be deleted. So they decided to troll the OP with no information about why that code is useful... Frequently such duplicate answers downvoted as not useful... not sure if that's going to happen here. – Alexei Levenkov Feb 15 '19 at 22:49
  • @AlexeiLevenkov, well, I think that they are just a new user and need a bit of guidance. Also, Please check my comment to the post why I think it's not a duplicate (btw, did you see notification about it? I first didn't include your nick properly, and I'm not sure how it works). –  Feb 15 '19 at 22:54
  • Hi @dyukha , indeed i'm new to answer questions here on this user. haven't done so for like 10 years , but I'll take your advice next time , any way i think in that case the code was self explanatory. – Gilad Guy Feb 17 '19 at 01:19
0

In addition to the previous answers: new List<List<int>>(Cards) doesn't do what you think it does. It sets capacity, not elementCount (or whatever it's called). When bounds are checked, elementCount is used, not capacity. capacity is useful when you have a good idea how many elements you have, to avoid reallocations and don't waste space.

So yes, before accessing by index you should add elements into the list manually.