Search code examples
c#exceptionformatexception

System.FormatException occurs when parsing text


I'm new here and new to C# so please be kind. I'm in the middle of an assignment for my programming class and can't for the life of me figure out the problem. The program is a calculator that takes input from two text boxes and operates on them as well as disables the second textbox and uses buttons to input the numbers (numberpad). I'm using Visual Studio 2013 and have gone over my code many times as well as stepped through it. Here is my code:

    static int checkFocusedTextbox = 1;
    string operationPerformed;
    double result = 0;

    private void Operator_Click(object sender, RoutedEventArgs e)
    {
       // try
      //  {
            Button button = (Button)sender;
            operationPerformed = button.Content.ToString();
            double num1 = double.Parse(inputOne.Text);
            double num2 = double.Parse(inputTwo.Text);

            if (checkFocusedTextbox == 1)
            {
                if ((inputOne.Text == "") || (inputTwo.Text == ""))
                {
                    error.Text = "Please fill both text fields";
                }
                else if ((inputOne.Text != "") || (inputTwo.Text != ""))
                {
                    switch ((string)button.Tag)
                    {
                        case "+":
                            inputOne.Clear();
                            inputTwo.Focus();
                            result = num1 + num2;
                            break;
                        case "-":
                            inputOne.Clear();
                            num2 = double.Parse(inputOne.Text);
                            result = num1 + num2;
                            break;
                        case "*":
                            inputOne.Clear();
                            num2 = double.Parse(inputOne.Text);
                            result = num1 + num2;
                            break;
                        case "/":
                            inputOne.Clear();
                            num2 = double.Parse(inputOne.Text);
                            result = num1 / num2;
                            break;
                        case "%":
                            inputOne.Clear();
                            num2 = double.Parse(inputOne.Text);
                            result = num1 % num2;
                            break;
                        default:
                            break;
                    }
                    output.Text = result.ToString();
                }
            }
            else
            {
                Calculation(num1, num2);
                output.Text = result.ToString();
            }
    }

    public void Calculation(double number1, double number2)
    {
        switch (operationPerformed)
        {
            case "+":
                result = number1 + number2;
                break;
            case "-":
                result = number1 - number2;
                break;
            case "*":
                result = number1 * number2;
                break;
            case "/":
                result = number1 / number2;
                break;
            case "%":
                result = number1 % number2;
                break;
            default:
                break;
        }
    }

    public void CalculationNumberpad()
    {

    }

    private void NumberPad_Click(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;
        inputOne.Text += button.Content.ToString();
    }

    private void inputTwo_GotFocus(object sender, RoutedEventArgs e)
    {
        checkFocusedTextbox = 2;
    }

    private void inputOne_GotFocus(object sender, RoutedEventArgs e)
    {
        checkFocusedTextbox = 1;
    }
}

}

The Button class successfully retrieves the button click and heads into the operationPerformed variable. This has been declared at the top of my code as a string, so I can't understand why I get an error since I'm using ToString(). It then proceeds into the num1 double and gives me the error.

I'm in dire need of help and I know this community is THE best! Please forgive me if I have left out any information.

Thanks, Chris


Solution

  • You're doing this :

       double num1 = double.Parse(inputOne.Text);
       double num2 = double.Parse(inputTwo.Text);
    

    before you check whether the contents of inputOne or inputTwo are empty or not. If either is empty or contains a string which can't be parsed into a double, the Parse line will throw a System.FormatException.

    Take a look at double.TryParse to check whether something is parse-able without throwing an exception. So something like :

    double num1, num2;
    if (double.TryParse(inputOne.Text, out num1) && double.TryParse(inputTwo.Text, out num2))
    { ... values are valid, so do things}
    else
    { ... at least one value is not valid... warn user }