Programming question for c#
(Not sure where to put this, since it isn't quite a 'tech support' question, so general it is?)
So I'm brand spanking new to this whole programming thing (tbh I feel like idiot half the time forgetting things), and I'm having some trouble with something hopefully someone here could help me with.
The situation is this, I have a file I need to read/load data from and stick into a struct, and rather than being in a nice sequential list with 1 item per line its all in one string. (ie. C 12 D 12 H 3 D 5 S 6)
So what I have done is split it temporarily into an array (parts), than convert the string into an int and char, and load it into the struct (I have to use a struct) for future use.
I'm positive there is a better way to go about this than the ass backwards method I'm using, so if anyone has an idea it would be appreciated.
I've gone through a couple variants of this, but this is the one I have atm.
here is the parts I'm having trouble with:
things go tits up. So if anyone smarter than I knows a way around this, it'd be MUCHLY appreciated.
This is a different way I tried, but I always seem to get errors when converting string to int or char. The above is the only one that 'seems' to have worked, but only for the first two array elements. Here's a couple extra bits, but bare in mind I've changed some of the names of things.
Spoiler for extra's:
Here is a larger code block to give you more an idea of what I have going on. There is more after this that consists of methods, but I don't want to take up too much space.
Spoiler for code block:
Any help at all would be greatly appreciated.
Uh, how long are the 'lines' you're inputting? Your currentCard array is declared with a size of length = 10. In your for loop, your index goes from 0-9.
However, you blindly try to access parts[index] without first checking if that memory space is actually allocated. For example, in the sample line you gave, 'C 12 D 12 H 3 D 5 S 6' would return an array of 10 elements, however, based on your code, when your index is 10, you will try to access parts, which is actually the 11th element, which does not exist.
In fact, your mapping is incorrect too, as
currentCard.firstSuit is mapped to C
currentCard.firstFace is mapped to 12
but once you increase the index by 1
currentCard.firstSuit is mapped to 12
currentCard.firstFace is mapped to D
When it should be mapped to D, and then the next twelve. Ie, CurrenCard should be mapped to parts and parts . CurrentCard should be mapped to parts and parts. Instead of incrementing the index with index++, you should be incrementing by 2 every time, with index+= 2. This should get around the problem of exceeding the array allocation. However, that's assuming that you have at least 10 elements in the parts array. If it's dynamically sized, you will need to rewrite your loop based on the size of parts as opposed to the fixed length of currentCard.
Also, are you using an IDE of sorts? The debugger should tell you why it might be crashing. When you ask for help posting the error messages would be very useful.
Have you actually made the stride length a multiple of two?
currentCard[index].firstSuit = parts[index*2];
currentCard[index].firstFace = Convert.ToInt32(parts[(index*2)+1]);
@RobotCat index += 2 doesnt work since you would generate empty entries in the currentCard List...
If the for loop was build differently though it would work:
for (int index = 0; index < currentCard.Length*2; index+=2)
newEntry.firstSuit = Convert.ToChar(parts[index]);
newEntry.firstFace = Convert.ToInt32(parts[index+1]);
I would also wrap a try-catch block around it checking for conversion Exceptions, because its not clear if your input data is always correct. Trying to convert a char into int might not work resulting in an Exception.
And as RobotCat mentioned already... the length of the parts array may not always be a mutliple of two of currentCard.Length, I would make a plausibility check before using it.
edit: I had to edit the for loop or only half the cards would have been read in (see red change). And I dont think that char needs conversion...
Using VS 2010.
The error it was throwing was FormatException, input string not in correct format.
Doesn't surprise me that it's completely botched, I've probably re-written in like a dozen different way's I've probably confused myself into a knot.
Went and had lunch to clear my head (not that it takes much to do that), came back and went back over some notes. Think I found a 'better' way to do what I want, but shall try what you've suggested. Was trying to see what I could do from memory mostly, probably not the best idea for a newb.
I always forget about try-catch as well, derp@me.
Ah scratch my idea with the loop variant, because you already have a predefined currentCard array. Just go for the first thing with the stride length of two.
Besides, I would have written a line parser class that does all the magic of reading one line and used this to read files line by line in a file reader method in the main class.
Because you cannot optimize the way you read the file, but the line parsing is kind of a different concern (see design patter: "Seperation of Concerns")
edit: one more methodical thing I wanted to add
When you parse a line you should not constrain its length in the result array. But the length should be constraint by the actual input line. Something like
Ended up going with atm:
With that code you've got there Jinto, whats the % covering? Division isn't it? You last comment, I think was what was throwing me for a while to.
My last comment (I dont know exactly what part of my post is meant) might be wrong, because I wrote code without Visual Studio - completely freestyle (I do not even know if a ConversionException exists, I assume not... but it was meant to be an example, not working code). And I messed up with the conversion of Int32.
When I finished the code, I realized that it is kinda strange to convert a string to char, since indexing over a string returns chars.
So I wanted to eliminate the conversion from string to char (using parts[i]). However I mistakenly replaced the Int32 conversion. And even made the comment on the wrong place. :D
Btw. if you think, that simply replacing the end-condition in the for loop does the trick - thats not enough. Because your currentCard array still has a predefined size. If you let the deserializer/parser decide the size (number of entries) of currentCard, than it would be correct.
|All times are GMT -5. The time now is 14:20.|
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.