Question error: System.FormatException: 'Input string was not in a correct format

Nas

Member
Joined
Oct 13, 2018
Messages
5
Programming Experience
Beginner
I am getting a error when trying to create a Form that will randomly display 3 values of 3 Dice for 2 players and will compare the numbers and displays who won. The is running great but it started show me an error after I put the IF statement. The code runs but it returns the error when I click on "Play!" button.
The problem is that I am not getting any error in the "Error List" so the program is running but when I click on "Play" it freezes and pops up the error message "System.FormatException: 'Input string was not in a correct format" at the line:
int P1R = Convert.ToInt32(Player1_Result.Text); . I believe that my issue is in Int and String... I ran the debugger but not getting anywhere.
Can you please help... thanks
Dice1_input_Player1.Text = dice_Player1[0].ToString();
Dice2_input_Player1.Text = dice_Player1[1].ToString();
Dice3_input_Player1.Text = dice_Player1[2].ToString();

Dice1_input_Player2.Text = dice_Player2[0].ToString();
Dice2_input_Player2.Text = dice_Player2[1].ToString();
Dice3_input_Player2.Text = dice_Player2[2].ToString();

Player1_Result.Text = string.Format("Player 1: {0}{1}{2}", Dice1_input_Player1.Text, Dice2_input_Player1.Text, Dice3_input_Player1.Text);
Player2_Result.Text = string.Format("Player 2: {0}{1}{2}", Dice1_input_Player2.Text, Dice2_input_Player2.Text, Dice3_input_Player2.Text);

int P1R = Convert.ToInt32(Player1_Result.Text);
int P2R = Convert.ToInt32(Player2_Result.Text);

if (P1R >= P2R)
Console.Write("Player 1 Wins!");
else
Console.Write("Player 2 Wins!");
 
The error message is telling you that the input string was in the wrong format. The very first thing you should have done was look at the input string. Why didn't you? Presumably the exception is thrown here:
int P1R = Convert.ToInt32(Player1_Result.Text);

What is the input string there? It's Player1_Result.Text. Here's the code that constructs that value:
Player1_Result.Text = string.Format("Player 1: {0}{1}{2}", Dice1_input_Player1.Text, Dice2_input_Player1.Text, Dice3_input_Player1.Text);

The result of that is going to be something like "Player 1: 123". It should be fairly obvious why that can't be converted to an int.
 
I understand, thanks, the problem is that I am getting an error in the IF Statement saying that "Error CS0029 Cannot implicitly convert type 'string' to 'int'.

Player1_Result.Text = string.Format("Player 1: {0}{1}{2}", Dice1_input_Player1.Text, Dice2_input_Player1.Text, Dice3_input_Player1.Text);
Player2_Result.Text = string.Format("Player 2: {0}{1}{2}", Dice1_input_Player2.Text, Dice2_input_Player2.Text, Dice3_input_Player2.Text);


int PR1 = Player1_Result.Text;
int PR2 = Player2_Result.Text;


if (PR1 > PR2)
Player_Wins.Text = string.Format("Player 1 Wins! ", PR1);
else
Player_Wins.Text = string.Format("Player 2 Wins! ", PR2);
 
That's the same error but just in a different place. In what universe do you think that "Player 1: 123" is a valid representation of an integer? You can't convert a string to an int, either explicitly or implicitly, if that string doesn't contain a representation of valid integer.
 
Back
Top Bottom