Java BMICalculator does not work as expected












-1















I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns










share|improve this question


















  • 1





    Please do not post code as images.

    – CS_noob
    Nov 25 '18 at 3:53











  • I will copy and paste it next time instead, but using screenshots is easier. (-_-)

    – Anon5
    Nov 26 '18 at 5:15
















-1















I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns










share|improve this question


















  • 1





    Please do not post code as images.

    – CS_noob
    Nov 25 '18 at 3:53











  • I will copy and paste it next time instead, but using screenshots is easier. (-_-)

    – Anon5
    Nov 26 '18 at 5:15














-1












-1








-1


0






I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns










share|improve this question














I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 3:41









Anon5Anon5

32




32








  • 1





    Please do not post code as images.

    – CS_noob
    Nov 25 '18 at 3:53











  • I will copy and paste it next time instead, but using screenshots is easier. (-_-)

    – Anon5
    Nov 26 '18 at 5:15














  • 1





    Please do not post code as images.

    – CS_noob
    Nov 25 '18 at 3:53











  • I will copy and paste it next time instead, but using screenshots is easier. (-_-)

    – Anon5
    Nov 26 '18 at 5:15








1




1





Please do not post code as images.

– CS_noob
Nov 25 '18 at 3:53





Please do not post code as images.

– CS_noob
Nov 25 '18 at 3:53













I will copy and paste it next time instead, but using screenshots is easier. (-_-)

– Anon5
Nov 26 '18 at 5:15





I will copy and paste it next time instead, but using screenshots is easier. (-_-)

– Anon5
Nov 26 '18 at 5:15












4 Answers
4






active

oldest

votes


















0














As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.



The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.






share|improve this answer































    1















    • At the beginning the value of hm = 0 after evaluating line 6 ,7 and 8

    • Later when you use hm to calculate BMI you are doing a divide by 0 (because hm = 0), which results in BMI = NaN which java uses to refer to (+)(-)infinity or indeterminate type of results.

    • Later you think that you're calculating the BMI by assigning values to hm and hf but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI being printed which is NaN


    I'd advise you to go through an introductory programming course.






    share|improve this answer































      0














      Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.



      You are just allowed to assign value then calculate them.






      share|improve this answer































        0














        place the following statements



        double hm = (hm*12 + hi)/39.37;
        double wkg = wp/2.205;
        double BMI = wkg/(hm*hm);


        after the statement



        wp = keyboard.nextInt();





        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%2f53464462%2fjava-bmicalculator-does-not-work-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.



          The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.






          share|improve this answer




























            0














            As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.



            The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.






            share|improve this answer


























              0












              0








              0







              As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.



              The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.






              share|improve this answer













              As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.



              The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 25 '18 at 3:55









              CS_noobCS_noob

              4571311




              4571311

























                  1















                  • At the beginning the value of hm = 0 after evaluating line 6 ,7 and 8

                  • Later when you use hm to calculate BMI you are doing a divide by 0 (because hm = 0), which results in BMI = NaN which java uses to refer to (+)(-)infinity or indeterminate type of results.

                  • Later you think that you're calculating the BMI by assigning values to hm and hf but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI being printed which is NaN


                  I'd advise you to go through an introductory programming course.






                  share|improve this answer




























                    1















                    • At the beginning the value of hm = 0 after evaluating line 6 ,7 and 8

                    • Later when you use hm to calculate BMI you are doing a divide by 0 (because hm = 0), which results in BMI = NaN which java uses to refer to (+)(-)infinity or indeterminate type of results.

                    • Later you think that you're calculating the BMI by assigning values to hm and hf but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI being printed which is NaN


                    I'd advise you to go through an introductory programming course.






                    share|improve this answer


























                      1












                      1








                      1








                      • At the beginning the value of hm = 0 after evaluating line 6 ,7 and 8

                      • Later when you use hm to calculate BMI you are doing a divide by 0 (because hm = 0), which results in BMI = NaN which java uses to refer to (+)(-)infinity or indeterminate type of results.

                      • Later you think that you're calculating the BMI by assigning values to hm and hf but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI being printed which is NaN


                      I'd advise you to go through an introductory programming course.






                      share|improve this answer














                      • At the beginning the value of hm = 0 after evaluating line 6 ,7 and 8

                      • Later when you use hm to calculate BMI you are doing a divide by 0 (because hm = 0), which results in BMI = NaN which java uses to refer to (+)(-)infinity or indeterminate type of results.

                      • Later you think that you're calculating the BMI by assigning values to hm and hf but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI being printed which is NaN


                      I'd advise you to go through an introductory programming course.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 25 '18 at 3:58









                      RyotsuRyotsu

                      565314




                      565314























                          0














                          Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.



                          You are just allowed to assign value then calculate them.






                          share|improve this answer




























                            0














                            Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.



                            You are just allowed to assign value then calculate them.






                            share|improve this answer


























                              0












                              0








                              0







                              Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.



                              You are just allowed to assign value then calculate them.






                              share|improve this answer













                              Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.



                              You are just allowed to assign value then calculate them.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 25 '18 at 4:09









                              Geno ChenGeno Chen

                              2,3646925




                              2,3646925























                                  0














                                  place the following statements



                                  double hm = (hm*12 + hi)/39.37;
                                  double wkg = wp/2.205;
                                  double BMI = wkg/(hm*hm);


                                  after the statement



                                  wp = keyboard.nextInt();





                                  share|improve this answer






























                                    0














                                    place the following statements



                                    double hm = (hm*12 + hi)/39.37;
                                    double wkg = wp/2.205;
                                    double BMI = wkg/(hm*hm);


                                    after the statement



                                    wp = keyboard.nextInt();





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      place the following statements



                                      double hm = (hm*12 + hi)/39.37;
                                      double wkg = wp/2.205;
                                      double BMI = wkg/(hm*hm);


                                      after the statement



                                      wp = keyboard.nextInt();





                                      share|improve this answer















                                      place the following statements



                                      double hm = (hm*12 + hi)/39.37;
                                      double wkg = wp/2.205;
                                      double BMI = wkg/(hm*hm);


                                      after the statement



                                      wp = keyboard.nextInt();






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 25 '18 at 15:23









                                      LundinCast

                                      2,73641323




                                      2,73641323










                                      answered Nov 25 '18 at 4:00









                                      Priyadeep DattaPriyadeep Datta

                                      192




                                      192






























                                          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.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464462%2fjava-bmicalculator-does-not-work-as-expected%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

                                          Costa Masnaga

                                          Fotorealismo

                                          Sidney Franklin