Simple Calculator, System.FormatException












-3














I'm trying to make a simple calculator and its fully working but when I calculate without a = button, my program completely crashes and gives the error:




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




This is the code that it throws an error to:



second = double.Parse(aNumber);
// the strings and doubles:

String aNumber = "";
double first = 0.0;









share|improve this question
























  • Show us more code. What is second? How are you getting the user's value.
    – Frontear
    Nov 20 at 18:00










  • Yes, "" causes an exception. You can a) test for "" beforehand, or b) catch the exception.
    – Mr Lister
    Nov 20 at 18:04










  • double second = 0.0;
    – Pride UK
    Nov 20 at 18:14








  • 3




    You need to learn how to debug. Then learn Double.TryParse.
    – Dour High Arch
    Nov 20 at 18:16








  • 1




    Possible duplicate of Input string was not in a correct format
    – D Manokhin
    Nov 20 at 18:20
















-3














I'm trying to make a simple calculator and its fully working but when I calculate without a = button, my program completely crashes and gives the error:




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




This is the code that it throws an error to:



second = double.Parse(aNumber);
// the strings and doubles:

String aNumber = "";
double first = 0.0;









share|improve this question
























  • Show us more code. What is second? How are you getting the user's value.
    – Frontear
    Nov 20 at 18:00










  • Yes, "" causes an exception. You can a) test for "" beforehand, or b) catch the exception.
    – Mr Lister
    Nov 20 at 18:04










  • double second = 0.0;
    – Pride UK
    Nov 20 at 18:14








  • 3




    You need to learn how to debug. Then learn Double.TryParse.
    – Dour High Arch
    Nov 20 at 18:16








  • 1




    Possible duplicate of Input string was not in a correct format
    – D Manokhin
    Nov 20 at 18:20














-3












-3








-3







I'm trying to make a simple calculator and its fully working but when I calculate without a = button, my program completely crashes and gives the error:




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




This is the code that it throws an error to:



second = double.Parse(aNumber);
// the strings and doubles:

String aNumber = "";
double first = 0.0;









share|improve this question















I'm trying to make a simple calculator and its fully working but when I calculate without a = button, my program completely crashes and gives the error:




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




This is the code that it throws an error to:



second = double.Parse(aNumber);
// the strings and doubles:

String aNumber = "";
double first = 0.0;






c# calculator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 20:04









D Manokhin

588219




588219










asked Nov 20 at 17:57









Pride UK

1




1












  • Show us more code. What is second? How are you getting the user's value.
    – Frontear
    Nov 20 at 18:00










  • Yes, "" causes an exception. You can a) test for "" beforehand, or b) catch the exception.
    – Mr Lister
    Nov 20 at 18:04










  • double second = 0.0;
    – Pride UK
    Nov 20 at 18:14








  • 3




    You need to learn how to debug. Then learn Double.TryParse.
    – Dour High Arch
    Nov 20 at 18:16








  • 1




    Possible duplicate of Input string was not in a correct format
    – D Manokhin
    Nov 20 at 18:20


















  • Show us more code. What is second? How are you getting the user's value.
    – Frontear
    Nov 20 at 18:00










  • Yes, "" causes an exception. You can a) test for "" beforehand, or b) catch the exception.
    – Mr Lister
    Nov 20 at 18:04










  • double second = 0.0;
    – Pride UK
    Nov 20 at 18:14








  • 3




    You need to learn how to debug. Then learn Double.TryParse.
    – Dour High Arch
    Nov 20 at 18:16








  • 1




    Possible duplicate of Input string was not in a correct format
    – D Manokhin
    Nov 20 at 18:20
















Show us more code. What is second? How are you getting the user's value.
– Frontear
Nov 20 at 18:00




Show us more code. What is second? How are you getting the user's value.
– Frontear
Nov 20 at 18:00












Yes, "" causes an exception. You can a) test for "" beforehand, or b) catch the exception.
– Mr Lister
Nov 20 at 18:04




Yes, "" causes an exception. You can a) test for "" beforehand, or b) catch the exception.
– Mr Lister
Nov 20 at 18:04












double second = 0.0;
– Pride UK
Nov 20 at 18:14






double second = 0.0;
– Pride UK
Nov 20 at 18:14






3




3




You need to learn how to debug. Then learn Double.TryParse.
– Dour High Arch
Nov 20 at 18:16






You need to learn how to debug. Then learn Double.TryParse.
– Dour High Arch
Nov 20 at 18:16






1




1




Possible duplicate of Input string was not in a correct format
– D Manokhin
Nov 20 at 18:20




Possible duplicate of Input string was not in a correct format
– D Manokhin
Nov 20 at 18:20












2 Answers
2






active

oldest

votes


















0














b will be true or false if the try parse worked
d will contain the double or 0 if it fails
change anum to a valid number to test.



String anum = "";
double d = 0.0;
bool b = double.TryParse(anum, out d);





share|improve this answer





























    0














    double.Parse will throw an exception if the input is not valid. So you either need to use try catch - or the preferred way would be to use double.TryParse as below. The value of y below will be set to the value if TryParse returns true.



    class Program
    {
    static void Main(string args)
    {
    // This will cause an exception
    var someString = "SomeValue";
    var x = double.Parse(someString); // Comment this line out to run this example

    // This will work
    double y;
    if (double.TryParse(someString, out y))
    {
    Console.WriteLine(someString + " is a valid decimal");
    }
    else
    {
    Console.WriteLine(someString + " is not a valid decimal");
    }

    someString = "14.7";
    if (double.TryParse(someString, out y))
    {
    Console.WriteLine(someString + " is a valid decimal");
    }
    else
    {
    Console.WriteLine(someString + " is not a valid decimal");
    }
    }
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398837%2fsimple-calculator-system-formatexception%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      b will be true or false if the try parse worked
      d will contain the double or 0 if it fails
      change anum to a valid number to test.



      String anum = "";
      double d = 0.0;
      bool b = double.TryParse(anum, out d);





      share|improve this answer


























        0














        b will be true or false if the try parse worked
        d will contain the double or 0 if it fails
        change anum to a valid number to test.



        String anum = "";
        double d = 0.0;
        bool b = double.TryParse(anum, out d);





        share|improve this answer
























          0












          0








          0






          b will be true or false if the try parse worked
          d will contain the double or 0 if it fails
          change anum to a valid number to test.



          String anum = "";
          double d = 0.0;
          bool b = double.TryParse(anum, out d);





          share|improve this answer












          b will be true or false if the try parse worked
          d will contain the double or 0 if it fails
          change anum to a valid number to test.



          String anum = "";
          double d = 0.0;
          bool b = double.TryParse(anum, out d);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 18:16









          dcarl661

          276




          276

























              0














              double.Parse will throw an exception if the input is not valid. So you either need to use try catch - or the preferred way would be to use double.TryParse as below. The value of y below will be set to the value if TryParse returns true.



              class Program
              {
              static void Main(string args)
              {
              // This will cause an exception
              var someString = "SomeValue";
              var x = double.Parse(someString); // Comment this line out to run this example

              // This will work
              double y;
              if (double.TryParse(someString, out y))
              {
              Console.WriteLine(someString + " is a valid decimal");
              }
              else
              {
              Console.WriteLine(someString + " is not a valid decimal");
              }

              someString = "14.7";
              if (double.TryParse(someString, out y))
              {
              Console.WriteLine(someString + " is a valid decimal");
              }
              else
              {
              Console.WriteLine(someString + " is not a valid decimal");
              }
              }
              }





              share|improve this answer


























                0














                double.Parse will throw an exception if the input is not valid. So you either need to use try catch - or the preferred way would be to use double.TryParse as below. The value of y below will be set to the value if TryParse returns true.



                class Program
                {
                static void Main(string args)
                {
                // This will cause an exception
                var someString = "SomeValue";
                var x = double.Parse(someString); // Comment this line out to run this example

                // This will work
                double y;
                if (double.TryParse(someString, out y))
                {
                Console.WriteLine(someString + " is a valid decimal");
                }
                else
                {
                Console.WriteLine(someString + " is not a valid decimal");
                }

                someString = "14.7";
                if (double.TryParse(someString, out y))
                {
                Console.WriteLine(someString + " is a valid decimal");
                }
                else
                {
                Console.WriteLine(someString + " is not a valid decimal");
                }
                }
                }





                share|improve this answer
























                  0












                  0








                  0






                  double.Parse will throw an exception if the input is not valid. So you either need to use try catch - or the preferred way would be to use double.TryParse as below. The value of y below will be set to the value if TryParse returns true.



                  class Program
                  {
                  static void Main(string args)
                  {
                  // This will cause an exception
                  var someString = "SomeValue";
                  var x = double.Parse(someString); // Comment this line out to run this example

                  // This will work
                  double y;
                  if (double.TryParse(someString, out y))
                  {
                  Console.WriteLine(someString + " is a valid decimal");
                  }
                  else
                  {
                  Console.WriteLine(someString + " is not a valid decimal");
                  }

                  someString = "14.7";
                  if (double.TryParse(someString, out y))
                  {
                  Console.WriteLine(someString + " is a valid decimal");
                  }
                  else
                  {
                  Console.WriteLine(someString + " is not a valid decimal");
                  }
                  }
                  }





                  share|improve this answer












                  double.Parse will throw an exception if the input is not valid. So you either need to use try catch - or the preferred way would be to use double.TryParse as below. The value of y below will be set to the value if TryParse returns true.



                  class Program
                  {
                  static void Main(string args)
                  {
                  // This will cause an exception
                  var someString = "SomeValue";
                  var x = double.Parse(someString); // Comment this line out to run this example

                  // This will work
                  double y;
                  if (double.TryParse(someString, out y))
                  {
                  Console.WriteLine(someString + " is a valid decimal");
                  }
                  else
                  {
                  Console.WriteLine(someString + " is not a valid decimal");
                  }

                  someString = "14.7";
                  if (double.TryParse(someString, out y))
                  {
                  Console.WriteLine(someString + " is a valid decimal");
                  }
                  else
                  {
                  Console.WriteLine(someString + " is not a valid decimal");
                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 18:21









                  Jon Vote

                  3708




                  3708






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398837%2fsimple-calculator-system-formatexception%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen