# C# - System.FormatException error

This topic is 3749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi again folks. I'm at Uni now and we're using C# this year, not C++ as I thought we would be, but it's cool and I'm enjoying playing with C#. Now we've only covered the real absolute basics so far in lectures (Console.WriteLine(), variables, basic data types etc) so I'm kinda punching above my weight a bit with what I'm about to ask your help on, so please bear that in mind. I'm making a word scrambling program just for fun (and to show off next workshop session, naturally) and I reckon I'm nearly nearly there to having it work. However... there's this one problem I've spent the last few hours trying to fix. My code compiles but when I run it and enter a string and hit enter the program throws a fit and hits me with an "Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list." It doesn't tell me where this unhandled exception originates from in the code and I'm all out of ideas. I reckon it's something to do with an array, but I've had no luck changing things. Here's the code:
// Word Scrambler v0.0
// by Elof Valantor

// Takes string and scrambles any words of 4 or more letters,
// leaving the first and last letters intact.

using System;
using System.Text;

class WordScrambler
{
static void Main(string[] args)
{
Console.WriteLine("---## Word Scrambler ##---\n");
string str;

//array to hold words
string[] strArray = new string[255];
char[] splitter = {' '};

//split string into words
strArray = str.Split(splitter);

//check each word
for (int i = 0; i < strArray.Length; i++)
{
Random random = new Random();
StringBuilder strEdit = new StringBuilder(strArray[i]);
int strLength = strEdit.Length;

//check if word has 4 or more characters
if (strLength > 3)
{
int scrambleStart = 1;
[s]int scramblePos = scrambleStart;[/s]
int scrambleEnd = strEdit.Length - 1;
string strTemp = strEdit.ToString();

//scramble the middle letters
for (int j = 1; j < scrambleEnd; j++)
{
int ranNum = random.Next(scrambleStart, scrambleEnd);
char c = strEdit[j];
char r = strTemp[ranNum];
strEdit.Replace(c, r, j, 1);
} //end for
}
else
{
continue;
} //end if

strArray[i] = strEdit.ToString();
} //end for

//display new scrambled string
//note - this was a foreach loop. I changed it whilst trying to figure out what the bug is. if this doesn't have anything to do with the bug I'll change it back
for (int i = 0; i < strArray.Length; i++)
{
Console.Write("{1}{2}", strArray[i], " ");
} //end for
} //end method
} //end class

I plan to break it up into methods once it's working. And if some thing's really badly written... well, like I said, punching above my weight! The only thing I can think it might be is something to do with assigning strArray a size of [255] initially because the code is going to cycle through a ton of empty array locations. But I can't think of a way to determine exactly how many words the string input will produce without having an array with a pre-defined size. I'm not a where of any way to dynamically expand an array in C# atm. Any help is appreciated!  - Geez I've left a variable I'm no longer using in there. I'm such a noob... [Edited by - Elof Valantor on October 12, 2007 5:50:01 PM]

##### Share on other sites
Read the exception, it tells you exactly what the problem is. This line is non-kosher:
Console.Write("{1}{2}", strArray[i], " ");

Because 2 is an out of bound index. Valid indices when you pass two arguments are 0 and 1.

##### Share on other sites
Quote:
 Original post by Elof ValantorThe only thing I can think it might be is something to do with assigning strArray a size of [255] initially because the code is going to cycle through a ton of empty array locations. But I can't think of a way to determine exactly how many words the string input will produce without having an array with a pre-defined size.

Those 255 strings will actually be thrown away and not used at all. String.Split returns an array, you could replace this code:
string str;str = Console.ReadLine();string[] strArray = new string[255];char[] splitter = {' '};strArray = str.Split(splitter);

with one line:

string[] strArray = Console.ReadLine().Split( ' ' );

Also you don't have to declare arrays to work with functions that use params.

Edit:

Console.Write("{1}{2}", strArray[i], " ");

You don't have to pass everything as an argument here. This could also be shortened to:

Console.Write( "{0} ", strArray[i] );

##### Share on other sites
I can't believe I didn't spot that... I spent 2 hours trawling through changing anything and everything and it was so simple...

And about reading the exception. I'm still fresh to C# and I'm not quite fluent in interpreting stuff like that just yet. I'm one step closer now though.

I hate programming sometimes.

Thanks guys. *goes off to punish self*

##### Share on other sites
Quote:
 Original post by Elof ValantorI can't believe I didn't spot that... I spent 2 hours trawling through changing anything and everything and it was so simple...I hate programming sometimes.Thanks guys. *goes off to punish self*

But if it was easy you couldnt show it off now could you?

##### Share on other sites

This topic is 3749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.