Attempting to generate a alphabetic character string of varied length but
am unable to successfully output
I've seen posts similar to the question I am asking but I was unable to
either understand or extract a solution that I am able to implement from
them. My goal is to generate a string of alphabetic characters based on
varied random length and pad the generated values with spaces, zero's and
other characters.
As you can hopefully see from the below code, I am passing in a value,
("Left" or "Right") to the FirstNameGenerator(). It's an "object" type
because the value is retrieved from an enum. I've instantiated a random
object, created a string of alphabetic characters, set an integer variable
("size") to a value between 0 and 35, created a char array and passed in
the ("size") random integer value and created a StreamWriter to write the
data out to a file.
The issue is, the padding is not getting added to the generated value. The
random data generated is perfect for what I need (junk test data) but I
just need to get the padding added to the generated values and I'd be
thrilled. I've added the PadRight/PadLeft method to the "return" statement
as well, "return new string(buffer).PadRight(paddingAmount,
paddingCharacter);" but that didn't work. Still got the random generated
value without the padding. I also tried adding it in the main method after
it was returned but it failed to add the padding to the output there too.
Any guidance would be deeply appreciated. I am just starting out with
code. This is pretty much my first attempt and I've been at for 2 days
now. I am sure there are a lot better ways to accomplish this task that
more experienced coders could rattle off in a second so if anyone has any
input, guidance or constructive criticism - I'm all ears.
Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RondomGenerators
{
class RandomGenerators
{
public string FirstNameGenerator(int paddingAmount, char
paddingCharacter, object paddingDirection)
{
Random rand = new Random();
const string alphaChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int size = rand.Next(0, 35);
char[] buffer = new char[size];
using (System.IO.StreamWriter file = new
System.IO.StreamWriter(@"F:\RondomGenerators\RondomGenerators\GenData\output.txt",
false))
if (paddingDirection.ToString().Contains("Right"))
{
for (int i = 0; i < size; i++)
buffer[i] = alphaChars[rand.Next(alphaChars.Length)];
new string(buffer).PadRight(paddingAmount, paddingCharacter);
file.WriteLine(buffer);
}
else
{
for (int i = 0; i < size; i++)
buffer[i] = alphaChars[rand.Next(alphaChars.Length)];
new string(buffer).PadLeft(paddingAmount, paddingCharacter);
file.WriteLine(buffer);
}
return new string(buffer);
}
}
}
IN MAIN:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace RondomGenerators
{
class Program
{
enum paddingDirection { Right, Left };
static void Main(string[] args)
{
RandomGenerators fname = new RandomGenerators();
Console.WriteLine(fname.FirstNameGenerator(15, 'X',
paddingDirection.Right));
Console.ReadLine();
}
}
}
No comments:
Post a Comment