Question

Im making a treegenerator just for training and im taking input from the console splitting it into an array but while trying to split i get an error saying System.FormatException(not correct input type)

Console.WriteLine("Input Number of branches, treefoot, and branch char");
string[] input = Console.ReadLine().Split(' ');
int nb = int.Parse(input[1]);
char tf = char.Parse(input[2]);
char bc = char.Parse(input[3]);
Was it helpful?

Solution

Your index 0 is not an int in a string, but text in a string.

You also got your indices wrong, use 0,1,2

int nb;
bool ok = int.TryParse(input[0], out nb); // ok is true if parsing succeeded
char tf = char.Parse(input[1]);
char bc = char.Parse(input[2]);

You should use int.tryParse() to see if the int parsing succeeded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top