C
C#11mo ago
d3adin.

✅ Console Readline accepting empty value.

Hi all, I'm trying to set console readline to not accept empty value (""). However, I've tried a bunch of different options and I still get "System.FormatException: 'The input string '' was not in a correct format.' " Here is my code
do
{
Console.WriteLine("Player 1, how far away from the city do you want to station the Manticore? ");
manticoreDistance = Convert.ToInt32(readResult);
} while (manticoreDistance < 0 || manticoreDistance > 100);
Console.Clear();
do
{
Console.WriteLine("Player 1, how far away from the city do you want to station the Manticore? ");
manticoreDistance = Convert.ToInt32(readResult);
} while (manticoreDistance < 0 || manticoreDistance > 100);
Console.Clear();
Ideally the goal was to make a loop that displays a message stating that a number needs to be entered.
8 Replies
𝔄ℑ𝔇𝔈𝔑
Im not sure if understood correctly i think u mean if (manticoreDistance == string.Empty) { // handle it }
d3adin.
d3adin.OP11mo ago
I will give this a try to see if it works. I wasn't sure how to explain it
cap5lut
cap5lut11mo ago
a much simpler option would be to simply use $tryparse btw
MODiX
MODiX11mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
cap5lut
cap5lut11mo ago
would be something like
Console.WriteLine("Player 1, how far away from the city do you want to station the Manticore?");
int manticoreDistance;
while (!int.TryParse(Console.ReadLine(), out manticoreDistance) || manticoreDistance < 0 || manticoreDistance > 100)
{
Console.WriteLine("Input was incorrect. Please enter a distance between 0 and 100.");
}
// manticoreDistance is >= 0 and <= 100
Console.WriteLine("Player 1, how far away from the city do you want to station the Manticore?");
int manticoreDistance;
while (!int.TryParse(Console.ReadLine(), out manticoreDistance) || manticoreDistance < 0 || manticoreDistance > 100)
{
Console.WriteLine("Input was incorrect. Please enter a distance between 0 and 100.");
}
// manticoreDistance is >= 0 and <= 100
d3adin.
d3adin.OP11mo ago
Thank you so much! This works exactly how I wanted it to work! I didn't know you can add Console.ReadLine to the loop condition 😮
cap5lut
cap5lut11mo ago
glad i could help o7 if ur question is answered dont forget to $close the channel
MODiX
MODiX11mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server