Share via


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

Question

Saturday, October 13, 2018 7:03 PM

Hi there,

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!");

All replies (7)

Saturday, October 13, 2018 7:22 PM

A FormatException in "Convert.ToInt32(Player1_Result.Text)" means that Player1_Result.Text did not contain a correct text that could be interpreted as an Int32. A common mistake is to leave the textbox blank. This is NOT interpreted as zero, it throws an error. Also, anything else that is not an integer such as letters, a decimal point or spaces will throw a formatException. Use the debugger and place a breakpoint in that line and examine the text that you are trying to convert to see what's in there.


Saturday, October 13, 2018 8:23 PM

Hello, 

Instead of 

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

Use

int P1R = 0;
if(!int.TryParse(Player1_Result.Text.Trim(), out P1R))
{
// error detected!!! need to handle some how
}

Sincerely, Highly skilled coding monkey.


Saturday, October 13, 2018 11:56 PM

thanks for your help,

I ended up with this:

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.TryParse(Player1_Result.Text, out int PR1);
            int.TryParse(Player2_Result.Text, out int PR2);

            if (PR1 > PR2)
                Player_Wins.Text = string.Format("Player 1 Wins! ", PR1);
            else
                Player_Wins.Text = string.Format("Player 2 Wins! ", PR2);

I need the IF Statement for comparing the results of the two players and displaying the message (Player 1 Wins or Player two Wins)... the numbers come out randomly... but I am getting always Player 2 Wins displayed if though Player 1 throws higher than Player 2. Please see attached file... https://imgur.com/a/t2K7TXQ notice that Player 1 > Player 2 but getting Player 2 Wins. I can't figure out what is missing because I am not getting an error message and debugging is not helping.

Can you please help?

Thanks


Sunday, October 14, 2018 12:45 AM

                Player_Wins.Text = string.Format("Player 1 Wins! ", PR1);
            else
                Player_Wins.Text = string.Format("Player 2 Wins! ", PR2);

That can't be your actual code. You must have typed it in. Don't do that.
Use Windows Copy & Paste to copy the real code from the IDE window.

Those lines are missing {0}

  • Wayne

Sunday, October 14, 2018 5:08 AM

                Player_Wins.Text = string.Format("Player 1 Wins! ", PR1);
            else
                Player_Wins.Text = string.Format("Player 2 Wins! ", PR2);

That can't be your actual code. You must have typed it in. Don't do that.
Use Windows Copy & Paste to copy the real code from the IDE window.

Those lines are missing {0}

  • Wayne

Actually Wayne, that will run OK … no compiler or runtime errors (kind of weird, there should be errors … but it worked fine in VS 2017 for me.

But, Nas-ak, your problem is here:

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

int.TryParse(Player1_Result.Text, out int PR1);

// and the same for Player2

You are trying to Parse a string that contains the string "Player 1: 2 2 5"  (going by the image you posted). Guess what? That's obviously not a valid number, so the result of the TryParse to PR1 will be 0.  And the same for PR2.  Which, of course means that PR1 > PR2 will always be false and you'll always see Player 2 Wins!

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Monday, October 15, 2018 1:22 PM

Thanks Bonnie,

How do you suggest I should change it? I appreciate your help.

Regards


Monday, October 15, 2018 1:55 PM

How do you suggest I should change it? 

That should be rather clear from what you have been told already.
The string you are converting to an integer must represent a valid
number only. So construct strings with just the numeric values
in them - from the "{0}{1}{2}" part. Convert and compare - without
the alpha prefix.

There's nothing stopping you from creating strings to compare,
and strings to display, from the same data if you want.

  • Wayne