multiplying 2 digits in TextField - Java FX











up vote
0
down vote

favorite












I am struggling with a bug in my application.
General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price



The problem is that is I switch slider again math operations are accumulating,
for Example I switched slider to 2, then to 3, after that to 4



My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?



daysSlider.valueProperty().addListener(new ChangeListener() {



        @Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {

int i = Integer.parseInt(priceTextField.getText());


priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue()* i ));


}

});









share|improve this question




























    up vote
    0
    down vote

    favorite












    I am struggling with a bug in my application.
    General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price



    The problem is that is I switch slider again math operations are accumulating,
    for Example I switched slider to 2, then to 3, after that to 4



    My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?



    daysSlider.valueProperty().addListener(new ChangeListener() {



            @Override
    public void changed(ObservableValue arg0, Object arg1, Object arg2) {

    int i = Integer.parseInt(priceTextField.getText());


    priceTextField.textProperty().setValue(
    String.valueOf((int) daysSlider.getValue()* i ));


    }

    });









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am struggling with a bug in my application.
      General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price



      The problem is that is I switch slider again math operations are accumulating,
      for Example I switched slider to 2, then to 3, after that to 4



      My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?



      daysSlider.valueProperty().addListener(new ChangeListener() {



              @Override
      public void changed(ObservableValue arg0, Object arg1, Object arg2) {

      int i = Integer.parseInt(priceTextField.getText());


      priceTextField.textProperty().setValue(
      String.valueOf((int) daysSlider.getValue()* i ));


      }

      });









      share|improve this question















      I am struggling with a bug in my application.
      General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price



      The problem is that is I switch slider again math operations are accumulating,
      for Example I switched slider to 2, then to 3, after that to 4



      My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?



      daysSlider.valueProperty().addListener(new ChangeListener() {



              @Override
      public void changed(ObservableValue arg0, Object arg1, Object arg2) {

      int i = Integer.parseInt(priceTextField.getText());


      priceTextField.textProperty().setValue(
      String.valueOf((int) daysSlider.getValue()* i ));


      }

      });






      java javafx






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 16:07

























      asked Nov 11 at 14:51









      Tomek Młynarski

      66




      66
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          So the problem that you are having is getting the String from the Text Field and doing math operations with it.



          The way that you can go around this problem is by parsing the String to an Integer by doing the following:



          int baseCost = Integer.parseInt(textField.getValue());


          This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField by doing the following:



          //totalCost is an integer value calculated by multiplying the base cost and the slider's value.
          priceTextField.textProperty().setValue(Integer.toString(totalCost));


          I hope this helps!



          With your new problem, this is why it is happening:



          The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField to daySlider times i. Then, the next time that there is a change, you are setting i equal to the value currently in the priceTextField. This is why it is doing multiple multiplications.



          If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue()) to get the value for your base cost. Then, you can multiply it with the daysSlider value and do priceTextField.textProperty().setValue(
          Integer.toString(i * sliderValue);



          To solve your new problem:



          ArrayList<Integer> baseCost = new ArrayList<>();

          priceTextField.valueProperty().addListener(new ChangeListener(){
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          baseCost.add(Integer.parseInt(priceTextField.getText()));
          }
          }
          daysSlider.valueProperty().addListener(new ChangeListener() {

          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }

          });


          ANSWER to your 3rd question (can be found in the chat):



          This is how you make a runnable:



          daysSlider.valueProperty().addListener(new ChangeListener() {
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          Runnable r = new Runnable() {
          @Override
          public void run() {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }
          };

          Handler handler = new Handler();
          //Run the runnable after 3000 milliseconds or 3 seconds.
          handler.postDelayed(runnable, 3000);

          }

          });





          share|improve this answer























          • Comments are not for extended discussion; this conversation has been moved to chat.
            – Samuel Liew
            Nov 11 at 22:37











          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',
          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%2f53249882%2fmultiplying-2-digits-in-textfield-java-fx%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          So the problem that you are having is getting the String from the Text Field and doing math operations with it.



          The way that you can go around this problem is by parsing the String to an Integer by doing the following:



          int baseCost = Integer.parseInt(textField.getValue());


          This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField by doing the following:



          //totalCost is an integer value calculated by multiplying the base cost and the slider's value.
          priceTextField.textProperty().setValue(Integer.toString(totalCost));


          I hope this helps!



          With your new problem, this is why it is happening:



          The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField to daySlider times i. Then, the next time that there is a change, you are setting i equal to the value currently in the priceTextField. This is why it is doing multiple multiplications.



          If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue()) to get the value for your base cost. Then, you can multiply it with the daysSlider value and do priceTextField.textProperty().setValue(
          Integer.toString(i * sliderValue);



          To solve your new problem:



          ArrayList<Integer> baseCost = new ArrayList<>();

          priceTextField.valueProperty().addListener(new ChangeListener(){
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          baseCost.add(Integer.parseInt(priceTextField.getText()));
          }
          }
          daysSlider.valueProperty().addListener(new ChangeListener() {

          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }

          });


          ANSWER to your 3rd question (can be found in the chat):



          This is how you make a runnable:



          daysSlider.valueProperty().addListener(new ChangeListener() {
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          Runnable r = new Runnable() {
          @Override
          public void run() {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }
          };

          Handler handler = new Handler();
          //Run the runnable after 3000 milliseconds or 3 seconds.
          handler.postDelayed(runnable, 3000);

          }

          });





          share|improve this answer























          • Comments are not for extended discussion; this conversation has been moved to chat.
            – Samuel Liew
            Nov 11 at 22:37















          up vote
          0
          down vote



          accepted










          So the problem that you are having is getting the String from the Text Field and doing math operations with it.



          The way that you can go around this problem is by parsing the String to an Integer by doing the following:



          int baseCost = Integer.parseInt(textField.getValue());


          This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField by doing the following:



          //totalCost is an integer value calculated by multiplying the base cost and the slider's value.
          priceTextField.textProperty().setValue(Integer.toString(totalCost));


          I hope this helps!



          With your new problem, this is why it is happening:



          The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField to daySlider times i. Then, the next time that there is a change, you are setting i equal to the value currently in the priceTextField. This is why it is doing multiple multiplications.



          If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue()) to get the value for your base cost. Then, you can multiply it with the daysSlider value and do priceTextField.textProperty().setValue(
          Integer.toString(i * sliderValue);



          To solve your new problem:



          ArrayList<Integer> baseCost = new ArrayList<>();

          priceTextField.valueProperty().addListener(new ChangeListener(){
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          baseCost.add(Integer.parseInt(priceTextField.getText()));
          }
          }
          daysSlider.valueProperty().addListener(new ChangeListener() {

          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }

          });


          ANSWER to your 3rd question (can be found in the chat):



          This is how you make a runnable:



          daysSlider.valueProperty().addListener(new ChangeListener() {
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          Runnable r = new Runnable() {
          @Override
          public void run() {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }
          };

          Handler handler = new Handler();
          //Run the runnable after 3000 milliseconds or 3 seconds.
          handler.postDelayed(runnable, 3000);

          }

          });





          share|improve this answer























          • Comments are not for extended discussion; this conversation has been moved to chat.
            – Samuel Liew
            Nov 11 at 22:37













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          So the problem that you are having is getting the String from the Text Field and doing math operations with it.



          The way that you can go around this problem is by parsing the String to an Integer by doing the following:



          int baseCost = Integer.parseInt(textField.getValue());


          This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField by doing the following:



          //totalCost is an integer value calculated by multiplying the base cost and the slider's value.
          priceTextField.textProperty().setValue(Integer.toString(totalCost));


          I hope this helps!



          With your new problem, this is why it is happening:



          The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField to daySlider times i. Then, the next time that there is a change, you are setting i equal to the value currently in the priceTextField. This is why it is doing multiple multiplications.



          If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue()) to get the value for your base cost. Then, you can multiply it with the daysSlider value and do priceTextField.textProperty().setValue(
          Integer.toString(i * sliderValue);



          To solve your new problem:



          ArrayList<Integer> baseCost = new ArrayList<>();

          priceTextField.valueProperty().addListener(new ChangeListener(){
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          baseCost.add(Integer.parseInt(priceTextField.getText()));
          }
          }
          daysSlider.valueProperty().addListener(new ChangeListener() {

          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }

          });


          ANSWER to your 3rd question (can be found in the chat):



          This is how you make a runnable:



          daysSlider.valueProperty().addListener(new ChangeListener() {
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          Runnable r = new Runnable() {
          @Override
          public void run() {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }
          };

          Handler handler = new Handler();
          //Run the runnable after 3000 milliseconds or 3 seconds.
          handler.postDelayed(runnable, 3000);

          }

          });





          share|improve this answer














          So the problem that you are having is getting the String from the Text Field and doing math operations with it.



          The way that you can go around this problem is by parsing the String to an Integer by doing the following:



          int baseCost = Integer.parseInt(textField.getValue());


          This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField by doing the following:



          //totalCost is an integer value calculated by multiplying the base cost and the slider's value.
          priceTextField.textProperty().setValue(Integer.toString(totalCost));


          I hope this helps!



          With your new problem, this is why it is happening:



          The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField to daySlider times i. Then, the next time that there is a change, you are setting i equal to the value currently in the priceTextField. This is why it is doing multiple multiplications.



          If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue()) to get the value for your base cost. Then, you can multiply it with the daysSlider value and do priceTextField.textProperty().setValue(
          Integer.toString(i * sliderValue);



          To solve your new problem:



          ArrayList<Integer> baseCost = new ArrayList<>();

          priceTextField.valueProperty().addListener(new ChangeListener(){
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          baseCost.add(Integer.parseInt(priceTextField.getText()));
          }
          }
          daysSlider.valueProperty().addListener(new ChangeListener() {

          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }

          });


          ANSWER to your 3rd question (can be found in the chat):



          This is how you make a runnable:



          daysSlider.valueProperty().addListener(new ChangeListener() {
          @Override
          public void changed(ObservableValue arg0, Object arg1, Object arg2) {
          Runnable r = new Runnable() {
          @Override
          public void run() {
          priceTextField.textProperty().setValue(
          String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
          }
          };

          Handler handler = new Handler();
          //Run the runnable after 3000 milliseconds or 3 seconds.
          handler.postDelayed(runnable, 3000);

          }

          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 22:51

























          answered Nov 11 at 14:55









          Ishaan

          7131417




          7131417












          • Comments are not for extended discussion; this conversation has been moved to chat.
            – Samuel Liew
            Nov 11 at 22:37


















          • Comments are not for extended discussion; this conversation has been moved to chat.
            – Samuel Liew
            Nov 11 at 22:37
















          Comments are not for extended discussion; this conversation has been moved to chat.
          – Samuel Liew
          Nov 11 at 22:37




          Comments are not for extended discussion; this conversation has been moved to chat.
          – Samuel Liew
          Nov 11 at 22:37


















          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%2f53249882%2fmultiplying-2-digits-in-textfield-java-fx%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

          Full-time equivalent

          Bicuculline

          さくらももこ